xargs(1) CLIX xargs(1)
NAME
xargs - Constructs argument list(s) and executes commands
SYNOPSIS
xargs [flag ... ] [command [initial-arguments]]
FLAGS
The following three xargs flags determine how arguments are selected for
each command invocation. When none of these flags is specified, the
initial-arguments are followed by arguments read continuously from stdin
until an internal buffer is full, and then command is executed with the
accumulated args. This process is repeated until there are no more args.
When there are flag conflicts (for example, -l versus -n), the last flag
has precedence.
-l number command
Executes for each nonempty number lines of arguments from stdin.
The last invocation of command will be with fewer lines of
arguments if fewer than number remain. A line is considered to end
with the first newline unless the last character of the line is a
blank or a tab; a trailing blank/tab signals continuation through
the next nonempty line. If number is omitted, 1 is assumed. The
flag -x is forced.
-i replstr
Turns on insert mode, where command is executed for each line from
stdin, taking the entire line as a single arg, inserting it in
initial-arguments for each occurrence of replstr. A maximum of 5
arguments in initial-arguments may each contain one or more
instances of replstr. Blanks and tabs at the beginning of each
line are thrown away. Constructed arguments may not grow larger
than 255 characters, and flag -x is also forced. A set of braces
({}) is assumed for replstr if not specified.
-n number
Executes command using as many stdin arguments as possible, up to
number arguments maximum. Fewer arguments will be used if their
total size is greater than size characters, and for the last
invocation if there are fewer than number arguments remaining. If
flag -x is also specified, each number arguments must fit in the
size limitation, else xargs terminates execution.
Other valid xargs flags include the following:
-t Turns on trace mode, where the command and each constructed
argument list are echoed to file descriptor 2 just prior to
their execution.
2/94 - Intergraph Corporation 1
xargs(1) CLIX xargs(1)
-p Turns on prompt mode, where the user is asked whether to
execute command each invocation. Trace mode (-t) is turned on
to display the command instance to be executed, followed by a
?... prompt. A reply of y (optionally followed by anything)
will execute the command; anything else, including just a
carriage return, skips that particular invocation of command.
-x Causes xargs to terminate if any argument list would be
greater than size characters; -x is forced by the flags -i and
-l. When neither of the flags -i, -l, or -n are specified,
the total length of all arguments must be within the size
limit.
-s size Set the maximum total size of each argument list to size
characters; size must be a positive integer less than or equal
to 470. If -s is not specified, 470 is taken as the default.
Note that the character count for size includes one extra
character for each argument and the count of characters in the
command name.
-e eofstr Takes the eofstr parameter as the logical end-of-file string.
Underbar (_) is assumed for the logical EOF string if -e is
not specified. The value -e with no eofstr specified turns
off the logical EOF string capability (underbar is taken
literally). The xargs command reads stdin until either end-
of-file or the logical EOF string is encountered.
DESCRIPTION
The xargs command combines the fixed initial-arguments with arguments read
from stdin to execute the specified command one or more times. The number
of arguments read for each command invocation and the manner in which they
are combined are determined by the flags specified.
The command parameter, which may be a shell file, is searched for, using
one's $PATH. If command is omitted, /bin/echo is used.
Arguments read in from stdin are defined to be contiguous strings of
characters delimited by one or more blanks, tabs, or newlines; empty lines
are always discarded. Blanks and tabs may be embedded as part of an
argument if escaped or quoted. Characters enclosed in quotes (single or
double) are taken literally, and the delimiting quotes are removed.
Outside of quoted strings a backslash (\) escapes the next character.
Each argument list is constructed starting with the initial-arguments,
followed by some number of arguments read from stdin (exception: see -i
flag).
The xargs command terminates if it receives a return code of -1 from
command, or if it cannot execute command. When command is a shell
program, it should explicitly exit (see sh) with an appropriate value to
2 Intergraph Corporation - 2/94
xargs(1) CLIX xargs(1)
avoid accidentally returning with -1.
EXAMPLES
1. To move all files from directory $1 to directory $2, and echo each
move command just before doing it:
ls $1 | xargs -i -t mv $1/{} $2/{}
2. To combine the output of the parenthesized commands onto one line,
which is then echoed to the end-of-file log:
(logname; date; echo $0 $*) | xargs >>log
3. To ask the user which files in the current directory are to be
archived and archive them into arch one at a time:
ls | xargs -p -l ar r arch
4. To ask the user which files in the current directory are to be
archived and archive them into arch many at a time:
ls | xargs -p -l | xargs ar r arch
5. To execute diff with successive pairs of arguments originally typed as
shell arguments:
echo $* | xargs -n2 diff
RELATED INFORMATION
Commands: sh(1)
2/94 - Intergraph Corporation 3