APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS














(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



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:



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




Click here to add your 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




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



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

Jump to Comments



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.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click 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!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Misc.




Related Posts

grep and ps

Grep in Depth

Grep awk example`

Wrapping scripts

How do I recursively grep through sub directories?

Xargs test question

How do I solve an "arglist too long"?

Grep -d --recurse

Xargs test question



Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress