home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: write Chapter 2
UNIX Commands
Next: Reference: yacc
 

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.

Options

-e string

Stop passing arguments when argument string is encountered (default is underscore).

-i

Pass arguments to command , replacing instances of { } on the command line with the current line of input.

-l n

Execute command for n lines of arguments.

-n n

Execute command with up to n arguments.

-p

Prompt for a y to confirm each execution of command .

-s n

Each argument list can contain up to n characters (470 is the default and the maximum value).

-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 would be invoked as a shell script, specifying filenames as arguments. Display file , one word per line (same as 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: write UNIX in a Nutshell: System V Edition Next: Reference: yacc
Reference: write Book Index Reference: yacc

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