Jump to content

Xargs: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
sublists large enough to be acceptable -> sublists small enough to be acceptable
No edit summary
Line 1: Line 1:
{{lowercase|title=xargs}}
{{lowercase|title=xargs}}
'''xargs''' is a command of [[Unix]] and most [[Unix-like]] [[operating system]]s. It is useful when one wants to pass a large number of arguments to a command. Arbitrary long lists of parameters can't be passed to a command,<ref>[http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long GNU Core Utilities FAQ]</ref> so xargs will break the list of arguments in sublists small enough to be acceptable.
'''xargs''' is a command of [[Unix]] and most [[Unix-like]] [[operating system]]s that is re loco. It is useful when one wants to pass a large number of arguments to a command. Arbitrary long lists of parameters can't be passed to a command,<ref>[http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long GNU Core Utilities FAQ]</ref> so xargs will break the list of arguments in sublists small enough to be acceptable.


For example, commands like:
For example, commands like:

Revision as of 04:31, 13 February 2008

xargs is a command of Unix and most Unix-like operating systems that is re loco. It is useful when one wants to pass a large number of arguments to a command. Arbitrary long lists of parameters can't be passed to a command,[1] so xargs will break the list of arguments in 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 in 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 like find, locate and grep.

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 string bar. These commands will not work as expected if there are whitespace characters, including newlines, 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

References

External links