Manages arguments passed to another command. This is often used either to increase efficiency (avoiding calling a command once for each argument) or to overcome shell environment limits ("Argument list too long"). It works by collecting arguments and invoking your command.
For example, given a script "tt" that simply echoes its argument count:
$ cat tt
echo $#
$ find . -print0 | xargs -0 ./tt
4469
3596
2658
5000
5000
4993
4846
5000
5000
5000
3416
(yes, I have a lot of files under my home directory)
I used "-print0" here because I have some screwed up file names here and there:
$ find . | xargs ./tt
5000
4080
5000
5000
xargs: unterminated quote
Documentation for xargs often suggests that it will pack the arguments as tightly as possible, so that the environment space is used to its maximum. That's not quite accurate, and better man pages will tell you how much packing will be done. For example, I could actually invoke "./tt" with all of the file names at once. To show this, I first had to clean up my list of file names:
$ find . | sed "s/\'//g;s/ //g" > ttt
That took care of the file names with "'"'s and spaces in them, and left me with a 48,982 line file:
$ wc -l ttt
48982 ttt
$ ./tt `cat ttt`
48982
Obviously xargs doesn't pack the command line as tightly as it could.
Sometimes you actually want to control the number of arguments, and xargs can do that. I once had a situation where we had a directory full of files that needed to be handled just two at a time. I forget now exactly why that was true; perhaps it was some limit because of the sizes of the files. Anyway, xargs has the answer:
find . -print0 | xargs -0 -n 2 ./tt
There are other useful flags; check the man page.
Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)
| Views for this page | ||||
|---|---|---|---|---|
| Today | This Week | This Month | This Year | Overall |
| 1 | 1 | 5 | 340 | 1,444 |
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
WordsoftheDayxargs :
"Sometimes you actually want to control the number of arguments, and xargs can do that. I once had a situation where we had a directory full of files that needed to be handled just two at a time. I forget now exactly why that was true; perhaps it was some limit because of the sizes of the files."
One use for that might be to run the files against diff.
--BigDumbDinosaur
Possibly. It might have been something where the files were named "abc.old" "abc.new" "xyz.old" "xyz.new" etc. I really don't remember, but it was probably something like that.
--TonyLawrence
Add your comments