Gentoo Wiki



What is xargs?

Xargs is one of those programs, like screen, where you don't see why it would be useful until you actually need it. Essentially, xargs passes arguments to a command, in a more efficient way. Linux, by default, allocates a maximum of 128 kB for arguments to a program. However, it is rather easy to go over this limit: running the command "rm *" in a very crowded directory would do it, for instance. There are a couple of ways to solve this, but the easiest by far is to use the find command to list the files in the directory, and pipe that to the xargs command, which can then call rm.

Installing xargs

Xargs will usually already be installed on your system, as a dependency for other stuff. It's included in the findutils package.

Why is this useful?

It's rather difficult to explain how this is useful, so I think a practical example is in order. Here's a quick example, involving finding out which package owns a given file.


for file in `ls /var/db/pkg/*/*/CONTENTS`
        grep $1 $file

find /var/db/pkg/ -iname CONTENTS | xargs grep $1

Code: Runtime comparison
ravi@3vil ~/bin $ time ./ /usr/bin/convert
obj /usr/bin/convert bffab47fb5d750c4716fda78b8f5cf1e 1143146309

real    0m0.529s
user    0m0.205s
sys     0m0.270s
ravi@3vil ~/bin $ time ./ /usr/bin/convert
/var/db/pkg/media-gfx/imagemagick- /usr/bin/convert bffab47fb5d750c4716fda78b8f5cf1e 1143146309

real    0m0.042s
user    0m0.014s
sys     0m0.023s

The difference in output is because grep does not print the filename if it is passed one input file. As you can see, the version written using xargs in place of a for loop is significantly faster.

More information

As always, see the man page: xargs man page

Retrieved from ""

Last modified: Fri, 12 Sep 2008 13:03:00 +0000 Hits: 3,221