2003/12/16 xargs

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:


Hate these ads?




$ 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.

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them.

I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.




Comments
"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

"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






Thu Jan 1 00:00:00 1970:  






Thu Jan 1 00:00:00 1970:  




Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar


Versatile Site Map Generator $49.00
A1 Sitemap Generator


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.

Publishing your articles here


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!





pavatar.jpg
More:




Related Posts

Xargs test question

How do I recursively grep through sub directories?

Wrapping scripts

Grep awk example`

grep and ps

Grep -d --recurse

Xargs test question

Grep in Depth

How do I solve an "arglist too long"?










Change Congress