Unleashed v1.4
[unleashed.git] / share / man / man1 / xargs.1
blob2647192818a091256cf34fd73d6143b86c57a3a1
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
46 .\"
47 .TH XARGS 1 "May 28, 2017"
48 .SH NAME
49 xargs \- construct argument lists and invoke utility
50 .SH SYNOPSIS
51 .LP
52 .nf
53 \fBxargs\fR [\fB-t\fR] [\fB-0\fR] [\fB-p\fR] [\fB-e\fR[\fIeofstr\fR]] [\fB-E\fR \fIeofstr\fR]
54      [\fB-I\fR \fIreplstr\fR] [\fB-i\fR[\fIreplstr\fR]] [\fB-L\fR \fInumber\fR] [\fB-l\fR[\fInumber\fR]]
55      [\fB-n\fR \fInumber\fR [\fB-x\fR]] [\fB-P\fR \fImaxprocs\fR] [\fB-s\fR \fIsize\fR]
56      [\fIutility\fR [\fIargument\fR...]]
57 .fi
59 .SH DESCRIPTION
60 .LP
61 The \fBxargs\fR utility constructs a command line consisting of the
62 \fIutility\fR and \fIargument\fR operands specified followed by as many
63 arguments read in sequence from standard input as fit in length and number
64 constraints specified by the options. The \fBxargs\fR utility then invokes the
65 constructed command line and waits for its completion. This sequence is
66 repeated until an end-of-file condition is detected on standard input or an
67 invocation of a constructed command line returns an exit status of \fB255\fR.
68 .sp
69 .LP
70 Arguments in the standard input must be separated by unquoted blank characters,
71 or unescaped blank characters or newline characters. A string of zero or more
72 non-double-quote (\fB"\fR) and non-newline characters can be quoted by
73 enclosing them in double-quotes. A string of zero or more non-apostrophe
74 (\fB\&'\fR) and non-newline characters can be quoted by enclosing them in
75 apostrophes. Any unquoted character can be escaped by preceding it with a
76 backslash (\fB\e\fR). The \fIutility\fR are executed one or more times until
77 the end-of-file is reached. The results are unspecified if the utility named by
78 \fIutility\fR attempts to read from its standard input.
79 .sp
80 .LP
81 The generated command line length is the sum of the size in bytes of the
82 utility name and each argument treated as strings, including a null byte
83 terminator for each of these strings. The \fBxargs\fR utility limits the
84 command line length such that when the command line is invoked, the combined
85 argument and environment lists can not exceed \fB{ARG_MAX}\(mi2048\fR bytes.
86 Within this constraint, if neither the \fB-n\fR nor the \fB-s\fR option is
87 specified, the default command line length is at least \fB{LINE_MAX}\fR.
88 .SH OPTIONS
89 .LP
90 The following options are supported:
91 .sp
92 .ne 2
93 .na
94 \fB\fB\fR\fB-e\fR\fB[\fR\fIeofstr\fR\fB]\fR\fR
95 .ad
96 .RS 15n
97 Uses \fIeofstr\fR as the logical end-of-file string. Underscore (\fB_\fR) is
98 assumed for the logical \fBEOF\fR string if neither \fB-e\fR nor \fB-E\fR is
99 used. When the \fIeofstr\fR option-argument is omitted, the logical \fBEOF\fR
100 string capability is disabled and underscores are taken literally. The
101 \fBxargs\fR utility reads standard input until either end-of-file or the
102 logical \fBEOF\fR string is encountered.
106 .ne 2
108 \fB\fB-E\fR \fIeofstr\fR\fR
110 .RS 15n
111 Specifies a logical end-of-file string to replace the default underscore.
112 \fBxargs\fR reads standard input until either end-of-file or the logical EOF
113 string is encountered. When \fIeofstr\fR is a null string, the logical
114 end-of-file string capability is disabled and underscore characters are taken
115 literally.
119 .ne 2
121 \fB\fB-I\fR \fIreplstr\fR\fR
123 .RS 15n
124 Insert mode. \fIutility\fR is executed for each line from standard input,
125 taking the entire line as a single argument, inserting it in \fIargument\fR
126 \fIs\fR for each occurrence of \fIreplstr\fR. A maximum of five arguments in
127 \fIargument\fRs can each contain one or more instances of \fIreplstr\fR. Any
128 blank characters at the beginning of each line are ignored. Constructed
129 arguments cannot grow larger than 255 bytes. Option \fB-x\fR is forced on. The
130 \fB-I\fR and \fB-i\fR options are mutually exclusive; the last one specified
131 takes effect.
135 .ne 2
137 \fB\fB\fR\fB-i\fR\fB[\fR\fIreplstr\fR\fB]\fR\fR
139 .RS 15n
140 This option is equivalent to \fB-I\fR \fIreplstr\fR. The string \fB{\|}\fR is
141 assumed for \fIreplstr\fR if the option-argument is omitted.
145 .ne 2
147 \fB\fB-L\fR \fInumber\fR\fR
149 .RS 15n
150 The \fIutility\fR is executed for each non-empty \fInumber\fR lines of
151 arguments from standard input. The last invocation of \fIutility\fR is with
152 fewer lines of arguments if fewer than \fInumber\fR remain. A line is
153 considered to end with the first newline character unless the last character of
154 the line is a blank character; a trailing blank character signals continuation
155 to the next non-empty line, inclusive. The \fB-L\fR, \fB-l\fR, and \fB-n\fR
156 options are mutually exclusive; the last one specified takes effect.
160 .ne 2
162 \fB\fB-l[\fR\fInumber\fR\fB]\fR\fR
164 .RS 15n
165 (The letter ell.) This option is equivalent to \fB-L\fR \fInumber\fR. If
166 \fInumber\fR is omitted, \fB1\fR is assumed. Option \fB-x\fR is forced on.
170 .ne 2
172 \fB\fB-n\fR \fInumber\fR\fR
174 .RS 15n
175 Invokes \fIutility\fR using as many standard input arguments as possible, up to
176 \fInumber\fR (a positive decimal integer) arguments maximum. Fewer arguments
177 are used if:
178 .RS +4
180 .ie t \(bu
181 .el o
182 The command line length accumulated exceeds the size specified by the \fB-s\fR
183 option (or \fB{LINE_MAX}\fR if there is no \fB-s\fR option), or
185 .RS +4
187 .ie t \(bu
188 .el o
189 The last iteration has fewer than \fInumber\fR, but not zero, operands
190 remaining.
195 .ne 2
197 \fB-p\fR
199 .RS 15n
200 Prompt mode. The user is asked whether to execute \fIutility\fR at each
201 invocation. Trace mode (\fB-t\fR) is turned on to write the command instance to
202 be executed, followed by a prompt to standard error. An affirmative response
203 (specific to the user's locale) read from \fB/dev/tty\fR executes the command;
204 otherwise, that particular invocation of \fIutility\fR is skipped.
208 .ne 2
210 \fB\fB-P\fR \fImaxprocs\fR\fR
212 .RS 15n
213 Invokes \fIutility\fR using at most \fImaxprocs\fR (a positive decimal integer)
214 parallel child processes.
218 .ne 2
220 \fB\fB-s\fR \fIsize\fR\fR
222 .RS 15n
223 Invokes \fIutility\fR using as many standard input arguments as possible
224 yielding a command line length less than \fIsize\fR (a positive decimal
225 integer) bytes. Fewer arguments are used if:
226 .RS +4
228 .ie t \(bu
229 .el o
230 The total number of arguments exceeds that specified by the \fB-n\fR option, or
232 .RS +4
234 .ie t \(bu
235 .el o
236 The total number of lines exceeds that specified by the \fB-L\fR option, or
238 .RS +4
240 .ie t \(bu
241 .el o
242 End of file is encountered on standard input before \fIsize\fR bytes are
243 accumulated.
245 Values of \fIsize\fR up to at least \fB{LINE_MAX}\fR bytes are supported,
246 provided that the constraints specified in DESCRIPTION are met. It is not
247 considered an error if a value larger than that supported by the implementation
248 or exceeding the constraints specified in DESCRIPTION is specified. \fBxargs\fR
249 uses the largest value it supports within the constraints.
253 .ne 2
255 \fB\fB-t\fR\fR
257 .RS 6n
258 Enables trace mode. Each generated command line is written to standard error
259 just prior to invocation.
263 .ne 2
265 \fB\fB-x\fR\fR
267 .RS 6n
268 Terminates if a command line containing \fInumber\fR arguments (see the
269 \fB-n\fR option above) or \fInumber\fR lines (see the \fB-L\fR option above)
270 does not fit in the implied or specified size (see the \fB-s\fR option above).
274 .ne 2
276 \fB-0\fR
278 .RS 6n
279 Null separator mode.  Instead of using white space or new lines to
280 delimit arguments, zero bytes are used.  This is suitable for use with
281 the -print0 argument to \fBfind\fR(1).
284 .SH OPERANDS
286 The following operands are supported:
288 .ne 2
290 \fB\fIutility\fR\fR
292 .RS 12n
293 The name of the utility to be invoked, found by search path using the
294 \fBPATH\fR environment variable. (ee \fBenviron\fR(5).) If \fIutility\fR is
295 omitted, the default is the \fBecho\fR(1) utility. If the \fIutility\fR operand
296 names any of the special built-in utilities in \fBshell_builtins\fR(1), the
297 results are undefined.
301 .ne 2
303 \fB\fIargument\fR\fR
305 .RS 12n
306 An initial option or operand for the invocation of \fIutility\fR.
309 .SH USAGE
311 The \fB255\fR exit status allows a utility being used by \fBxargs\fR to tell
312 \fBxargs\fR to terminate if it knows no further invocations using the current
313 data stream succeeds. Thus, \fIutility\fR should explicitly \fBexit\fR with an
314 appropriate value to avoid accidentally returning with \fB255\fR.
317 Notice that input is parsed as lines. Blank characters separate arguments. If
318 \fBxargs\fR is used to bundle output of commands like \fBfind\fR \fIdir\fR
319 \fB-print\fR or \fBls\fR into commands to be executed, unexpected results are
320 likely if any filenames contain any blank characters or newline characters.
321 This can be fixed by using \fBfind\fR to call a script that converts each file
322 found into a quoted string that is then piped to \fBxargs\fR. Notice that the
323 quoting rules used by \fBxargs\fR are not the same as in the shell. They were
324 not made consistent here because existing applications depend on the current
325 rules and the shell syntax is not fully compatible with it. An easy rule that
326 can be used to transform any string into a quoted form that \fBxargs\fR
327 interprets correctly is to precede each character in the string with a
328 backslash (\fB\e\fR).
331 On implementations with a large value for \fB{ARG_MAX}\fR, \fBxargs\fR can
332 produce command lines longer than \fB{LINE_MAX}\fR. For invocation of
333 utilities, this is not a problem. If \fBxargs\fR is being used to create a text
334 file, users should explicitly set the maximum command line length with the
335 \fB-s\fR option.
338 The \fBxargs\fR utility returns exit status \fB127\fR if an error occurs so
339 that applications can distinguish "failure to find a utility" from "invoked
340 utility exited with an error indication." The value \fB127\fR was chosen
341 because it is not commonly used for other meanings; most utilities use small
342 values for "normal error conditions" and the values above \fB128\fR can be
343 confused with termination due to receipt of a signal. The value \fB126\fR was
344 chosen in a similar manner to indicate that the utility could be found, but not
345 invoked.
346 .SH EXAMPLES
348 \fBExample 1 \fRUsing the xargs command
351 The following example moves all files from directory \fB$1\fR to directory
352 \fB$2\fR, and echo each move command just before doing it:
355 .in +2
357 example% \fBls $1 | xargs -I {} -t mv $1/{} $2/{}\fR
359 .in -2
364 The following command combines the output of the parenthesised commands onto
365 one line, which is then written to the end of file \fBlog\fR:
368 .in +2
370 example% \fB(logname; date; printf "%s\en" "$0 $*") | xargs >>log\fR
372 .in -2
377 The following command invokes \fBdiff\fR with successive pairs of arguments
378 originally typed as command line arguments (assuming there are no embedded
379 blank characters in the elements of the original argument list):
382 .in +2
384 example% \fBprintf "%s\en" "$*" | xargs -n 2 -x diff\fR
386 .in -2
391 The user is asked which files in the current directory are to be archived. The
392 files are archived into \fBarch\fR \fB;\fR a, one at a time, or b, many at a
393 time:
396 .in +2
398 example% \fBls | xargs -p -L 1 ar -r arch
399 ls | xargs -p -L 1 | xargs ar -r arch\fR
401 .in -2
406 The following executes with successive pairs of arguments originally typed as
407 command line arguments:
410 .in +2
412 example% \fBecho $* | xargs -n 2 diff\fR
414 .in -2
417 .SH ENVIRONMENT VARIABLES
419 See \fBenviron\fR(5) for descriptions of the following environment variables
420 that affect the execution of \fBxargs\fR: \fBLANG\fR, \fBLC_ALL\fR,
421 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
423 .ne 2
425 \fB\fBPATH\fR\fR
427 .RS 8n
428 Determine the location of \fIutility\fR.
433 Affirmative responses are processed using the extended regular expression
434 defined for the \fByesexpr\fR keyword in the \fBLC_MESSAGES\fR category of the
435 user's locale. The locale specified in the \fBLC_COLLATE\fR category defines
436 the behavior of ranges, equivalence classes, and multi-character collating
437 elements used in the expression defined for \fByesexpr\fR. The locale specified
438 in \fBLC_CTYPE\fR determines the locale for interpretation of sequences of
439 bytes of text data a characters, the behavior of character classes used in the
440 expression defined for the \fByesexpr\fR. See \fBlocale\fR(5).
441 .SH EXIT STATUS
443 The following exit values are returned:
445 .ne 2
447 \fB\fB0\fR\fR
449 .RS 12n
450 All invocations of \fIutility\fR returned exit status \fB0\fR.
454 .ne 2
456 \fB\fB1\(mi125\fR\fR
458 .RS 12n
459 A command line meeting the specified requirements could not be assembled, one
460 or more of the invocations of \fIutility\fR returned a non-zero exit status, or
461 some other error occurred.
465 .ne 2
467 \fB\fB126\fR\fR
469 .RS 12n
470 The utility specified by \fIutility\fR was found but could not be invoked.
474 .ne 2
476 \fB\fB127\fR\fR
478 .RS 12n
479 The utility specified by \fIutility\fR could not be found.
484 If a command line meeting the specified requirements cannot be assembled, the
485 utility cannot be invoked, an invocation of the utility is terminated by a
486 signal, or an invocation of the utility exits with exit status \fB255\fR, the
487 \fBxargs\fR utility writes a diagnostic message and exit without processing any
488 remaining input.
489 .SH ATTRIBUTES
491 See \fBattributes\fR(5) for descriptions of the following attributes:
496 box;
497 c | c
498 l | l .
499 ATTRIBUTE TYPE  ATTRIBUTE VALUE
501 CSI     Enabled
503 Interface Stability     Standard
506 .SH SEE ALSO
508 \fBecho\fR(1), \fBshell_builtins\fR(1), \fBattributes\fR(5), \fBenviron\fR(5),
509 \fBstandards\fR(5)