Xargs string interpolation, not by inserting a space

6

1

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

Jesvin Jose

Posted 2012-09-08T13:24:15.453

Reputation: 371

Answers

10

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.

user1686

Posted 2012-09-08T13:24:15.453

Reputation: 283 655

The find command gives me ./photos_each.html when I actually meant photos_each.html. I want simply the filename – Jesvin Jose – 2012-09-08T13:51:09.627

1@aitchnyu: They are equivalent here. Both ../template/photos_each.html and ../template/./photos_each.html should work. – user1686 – 2012-09-08T14:41:07.293

4

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
end

And here it is in bash:

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

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
end

Nicole Hamilton

Posted 2012-09-08T13:24:15.453

Reputation: 8 987

0

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

Ole Tange

Posted 2012-09-08T13:24:15.453

Reputation: 3 034