- Xargs
xargs(pronounced zargs) is a command of
Unix and mostUnix-like operating system s. 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, [ [http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long GNU Core Utilities FAQ] ] 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". However this version (functionally equivalent with "rm `find /path -type f`") will not fail:
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 '{}' +
xargs often covers the same functionality as the
backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a perfect companion for commands that output long lists of files likefind , locate andgrep .Examples
find . -name "*.foo" | xargs grep bar
does the same as
grep bar `find . -name "*.foo"`
Note that the preceding command uses backticks (`), not single quotes ('). It searches in all files in the current directory and its subdirectories which end in
.foo
for occurrences of the stringbar
. These commands will not work as expected if there are whitespace characters, includingnewline s, in the filenames. In order to avoid this limitation one may use:find . -name "*.foo" -print0 | xargs -0 grep bar
which uses GNU specific extensions to find and xargs to separate filenames using the
null character ;find . -name "*.foo" -print0 | xargs -0 -t -r vi
is similar to above, but launches the
vi editor for each of the files. The -t prints the command to stderr before issuing it. The -r is a GNU extension that tells xargs not to run the command if no input was received.find . -name "*.foo" -print0 | xargs -0 -I mv {} /tmp/trash
uses -I to tell xargs to replace {} with the argument list. Note that not all versions of xargs supports the {} syntax. In those cases you may specify a string after -I that will be replaced, e.g.
find . -name "*.foo" -print0 | xargs -0 -I xxx mv xxx /tmp/trash
uses string xxx instead of {} as the argument list marker.
find . -maxdepth 1 -type f -name "*.mp3" -print0 | xargs -0 -r cp -v -p --target-directory=/home/media
does the same as:
cp -v -p *.mp3 /home/media
however, the former command which uses find/xargs/cp is more resource efficient and will not halt with an error if the number of files is too large for the cp command to handle.
References
External links
* [http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs GNU man page on xargs]
Wikimedia Foundation. 2010.