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.



Comments /Words/2003_12_16.html
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

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
115340 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.

Publishing your articles here

pavatar.jpg
More:




Related Posts

How do I recursively grep through sub directories?

grep and ps

Grep in Depth

How do I solve an "arglist too long"?

Xargs test question

Xargs test question

Grep awk example`

Wrapping scripts

Grep -d --recurse


Unix/Linux Consultants

Your ad here - $24.00 yearly!

SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at http://opensystemscomputing.com and www.go2unix.com.


http://echo3.net/ Unix/Linux Custom Applications, Web Hosting, C/C++ Programming Courses


larryi@ccamedical.com SCO OS5, Debian Linux, RedHat Linux, MySQL, Apache, AJAX development using dXport/dL4/Unibasic, Windows Connectivity, Sharing Resouces, Automation, Shell Scripting









Change Congress


Related Posts