Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Final arguments for xargs

Tags:

xargs

I want to do something similar to this:

find . -type f | xargs cp dest_dir

But xargs will use dest_dir as initial argument, not as final argument. I would like to know:

  1. Is it possible to specify final arguments to xargs?
  2. Any other alternative to achieve the same result?

EDIT

A possible, but cumbersome alternative is this:

find . -type f | while read f ; do echo cp $f dest_dir ; done

I do not like this because dozens of cp processes will be started.

like image 341
blueFast Avatar asked Sep 06 '25 03:09

blueFast


2 Answers

-I option replaces occurrences of replace-str in the initial-arguments with names read from standard input.

For example:

find . -type f | xargs -I file cp file dest_dir

Should solve your problem.

More details in the following tutorial:

http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/

Especially the part about {} as the argument list marker

like image 132
Darth Hunterix Avatar answered Sep 07 '25 20:09

Darth Hunterix


I was trying to achieve something slightly different using xargs and pdfunite, and launching multiple instances of pdfunite didn't cut it for me.

I was essentially trying to do:

$ ls -v *.pdf | xargs pdfunite <files> all-files.pdf

with all-files.pdf being the final argument to pdfunite. But xargs with -I didn't give me what I wanted:

$ ls -v *.pdf | xargs -I {} pdfunite {} all-files.pdf

This just gave me a result which had all-files.pdf being the last file output by ls. And that's because multiple instances of pdfunite were launched.

I finally just gave up on xargs, and went with the following simpler construct, which sufficed for my purposes:

$ pdfunite `ls -v *.pdf` all-files.pdf

Hope this helps someone in future.

like image 29
Praveen Avatar answered Sep 07 '25 22:09

Praveen