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

3 Answers 3

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 Sep 8, 2012 at 13:51
  • 1
    @aitchnyu: They are equivalent here. Both ../template/photos_each.html and ../template/./photos_each.html should work. Sep 8, 2012 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .