Friday, July 26, 2013

XARGS



xargs is a command of Unix and most Unix-like operating systems. It is useful when one wants to pass a large number of arguments to a command. Arbitrarily long lists of parameters can't be passed to a command,so xargs will break the list of arguments into sublists small enough to be acceptable.

For example, commands like:
rm /path/*
rm `find /path -type f`
will fail with an error message of "Argument list too long" if there are too many files in /path.

find /path -type f -print0 | xargs -0 rm
In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist. This is more efficient than this functionally equivalent version:

find /path -type f -exec rm '{}' \;
which calls rm once for every single file. Note however that with modern versions of find, the following variant does the same thing as the xargs version:
find /path -type f -exec rm '{}' +

No comments:

Post a Comment