xargs
[options] [command]Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.
Without a command, xargs behaves similarly to echo, simply bundling the input lines into output lines and printing them to standard output.
-e
[string]Stop passing arguments when argument string is encountered (default is underscore). An omitted string disables the logical EOF capability.
-E
stringUse string instead of underscore as the default logical EOF string. Solaris only.
-i
[string]Pass arguments to command, replacing instances of {}
on the command line with the current line of input. With Solaris, the optional string can be used instead of {}
.
-I
stringSame as -i
, but string is used instead of {}
.
-l
[n]Execute command for n lines of arguments. With Solaris, default n is 1 when -l
is supplied.
-L
nSame as -l
n. Solaris only.
-n
nExecute command with up to n arguments.
-p
Prompt for a y
to confirm each execution of command. Implies -t
.
-s
nEach argument list can contain up to n characters. (Older systems limited n to 470. The default is system-dependent.)
-t
Echo each command before executing.
-x
Exit if argument list exceeds n characters (from -s
); -x
takes effect automatically with -i
and -l
.
grep for pattern in all files on the system:
find / -print | xargs grep
pattern> out &
Run diff on file pairs (e.g., f1.a and f1.b, f2.a and f2.b ...):
echo $* | xargs -n2 diff
The previous line could be invoked as a shell script, specifying filenames as arguments.
Display file, one word per line (similar to deroff -w
):
cat
file| xargs -n1
Move files in olddir to newdir, showing each command:
ls olddir | xargs -i -t mv olddir/{} newdir/{}