Understanding the UNIX command xargs

A good example of what xargs does is to try getting sorted checksums for every file in a directory using find.

find . | cksum  | sort

returns just one checksum, and it's not clear what it's the checksum for. Not what we want. The pipe sends the stdout from find into stdin for cksum. What cksum really wants is a list of command line args, e.g.

cksum file001.blah file002.blah  file003.blah

will report three lines, one per file, with the desired checksums. Xargs does the magic trick - converting stdout of the previous program to a temporary and hidden command line to feed to the next. The command line that works is:

find . | xargs cksum | sort

Note no pipe between xargs and cksum.


In general xargs is used like this

prog | xargs utility

where prog is expected to output one or more newline/space separated results. The trick is that xargs does not necessarily call utility once for each result, instead it splits the results into sublists and calls utility for every sublist. If you want to force xargs to call utility for every single result you will need to invoke it with xargs -L1.

Note that xargs promises you that the sublist sent to utility is shorter than ARG_MAX (If you're curious, you can get the current value of ARG_MAX using getconf ARG_MAX.) This is how it avoids those dreaded "Argument list to long" errors.

Tags:

Unix

Xargs