UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: whoamiChapter 2
Unix Commands
Next: Reference: xgettext
 

xargs

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.

Options

-e[string]

Stop passing arguments when argument string is encountered (default is underscore). An omitted string disables the logical EOF capability.

-E string

Use 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 string

Same 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 n

Same as -l n. Solaris only.

-nn

Execute command with up to n arguments.

-p

Prompt for a y to confirm each execution of command. Implies -t.

-sn

Each 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.

Examples

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/{}


Previous: Reference: whoamiUNIX in a Nutshell: System V EditionNext: Reference: xgettext
Reference: whoamiBook IndexReference: xgettext

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System