I want to repeat

touch ../template/filename

where filename comes from find. I want xargs to apply the touch operation, but I dont want it to insert a space like it usually does which derives into:

touch ../template/ filename

Here is my incomplete command. How Do I complete it?

find *.html | xargs touch ../template/WHAT-NOW
find -name "*.html" | xargs -d"\n" -I"{}" touch ../template/{}

find -name "*.html" -exec touch ../template/{} \;

Note that find *.html is wrong, since wildcards are expanded before command execution.

  • The find command gives me ./photos_each.html when I actually meant photos_each.html. I want simply the filename – Jesvin Jose Sep 8 '12 at 13:51
  • 1
    @aitchnyu: They are equivalent here. Both ../template/photos_each.html and ../template/./photos_each.html should work. – grawity Sep 8 '12 at 14:41

This is easier to write as a shell loop. For instance, in C shell you might write:

foreach i (`find -name "*.html"`)
   touch ../template/$i

And here it is in bash:

for i in `find -name "*.html"`
   touch ../template/$i

And in my Hamilton C shell (full disclosure: I'm the author), I added a "..." wildcard to tree-walk, so you could write:

foreach i (.../*.html)
   touch ../template/$i

Consider using GNU Parallel instead. That way you avoid nasty surprises if the filenames contain space ' or ":

find *.html | parallel -X touch ../template/{/}

See the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.