xref: /netbsd-src/usr.bin/xargs/xargs.1 (revision bee4003ac784a12b6c7810d99ebbf3549ef2936c)
1*bee4003aSnjoly.\" $NetBSD: xargs.1,v 1.23 2012/10/13 14:18:17 njoly Exp $
25bbd8ba4Sjtc.\"
35bbd8ba4Sjtc.\" Copyright (c) 1990, 1991, 1993
45bbd8ba4Sjtc.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" This code is derived from software contributed to Berkeley by
761f28255Scgd.\" John B. Roll Jr. and the Institute of Electrical and Electronics
861f28255Scgd.\" Engineers, Inc.
961f28255Scgd.\"
1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without
1161f28255Scgd.\" modification, are permitted provided that the following conditions
1261f28255Scgd.\" are met:
1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1461f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd.\"    documentation and/or other materials provided with the distribution.
1889aaa1bbSagc.\" 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd.\"    may be used to endorse or promote products derived from this software
2061f28255Scgd.\"    without specific prior written permission.
2161f28255Scgd.\"
2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd.\" SUCH DAMAGE.
3361f28255Scgd.\"
345bbd8ba4Sjtc.\"	@(#)xargs.1	8.1 (Berkeley) 6/6/93
3590cb07e9Schristos.\" $FreeBSD: src/usr.bin/xargs/xargs.1,v 1.40 2010/12/11 08:32:16 joel Exp $
36a173e99aSchristos.\" $xMach: xargs.1,v 1.2 2002/02/23 05:23:37 tim Exp $
3761f28255Scgd.\"
3890cb07e9Schristos.Dd December 21, 2010
392ab235c8Sjtc.Dt XARGS 1
402ab235c8Sjtc.Os
412ab235c8Sjtc.Sh NAME
422ab235c8Sjtc.Nm xargs
435bbd8ba4Sjtc.Nd "construct argument list(s) and execute utility"
442ab235c8Sjtc.Sh SYNOPSIS
454182cf29Slukem.Nm
4690cb07e9Schristos.Op Fl 0oprt
47a173e99aSchristos.Op Fl E Ar eofstr
48a173e99aSchristos.Oo
49a173e99aSchristos.Fl I Ar replstr
50a173e99aSchristos.Op Fl R Ar replacements
51a173e99aSchristos.Op Fl S Ar replsize
522ab235c8Sjtc.Oc
53a173e99aSchristos.Op Fl J Ar replstr
54a173e99aSchristos.Op Fl L Ar number
55a173e99aSchristos.Oo
56a173e99aSchristos.Fl n Ar number
57a173e99aSchristos.Op Fl x
58a173e99aSchristos.Oc
59a173e99aSchristos.Op Fl P Ar maxprocs
602ab235c8Sjtc.Op Fl s Ar size
61a173e99aSchristos.Op Ar utility Op Ar argument ...
622ab235c8Sjtc.Sh DESCRIPTION
6361f28255ScgdThe
644182cf29Slukem.Nm
65a173e99aSchristosutility reads space, tab, newline and end-of-file delimited strings
66a173e99aSchristosfrom the standard input and executes
672ab235c8Sjtc.Ar utility
68a173e99aSchristoswith the strings as
69a173e99aSchristosarguments.
702ab235c8Sjtc.Pp
71a173e99aSchristosAny arguments specified on the command line are given to
722ab235c8Sjtc.Ar utility
7361f28255Scgdupon each invocation, followed by some number of the arguments read
74a173e99aSchristosfrom the standard input of
75a173e99aSchristos.Nm .
76a173e99aSchristosThis is repeated until standard input is exhausted.
772ab235c8Sjtc.Pp
782ab235c8SjtcSpaces, tabs and newlines may be embedded in arguments using single
795bbd8ba4Sjtc(``\ '\ '')
805bbd8ba4Sjtcor double (``"'') quotes or backslashes (``\e'').
8161f28255ScgdSingle quotes escape all non-single quote characters, excluding newlines,
8261f28255Scgdup to the matching single quote.
8361f28255ScgdDouble quotes escape all non-double quote characters, excluding newlines,
8461f28255Scgdup to the matching double quote.
8561f28255ScgdAny single character, including newlines, may be escaped by a backslash.
862ab235c8Sjtc.Pp
8761f28255ScgdThe options are as follows:
88a173e99aSchristos.Bl -tag -width indent
894182cf29Slukem.It Fl 0
90a173e99aSchristosChange
91a173e99aSchristos.Nm
92a173e99aSchristosto expect NUL
934182cf29Slukem(``\e0'')
94a173e99aSchristoscharacters as separators, instead of spaces and newlines.
95a173e99aSchristosThis is expected to be used in concert with the
964182cf29Slukem.Fl print0
97a173e99aSchristosfunction in
984182cf29Slukem.Xr find 1 .
99a173e99aSchristos.It Fl E Ar eofstr
100a173e99aSchristosUse
101a173e99aSchristos.Ar eofstr
102a173e99aSchristosas a logical EOF marker.
103a173e99aSchristos.It Fl I Ar replstr
104a173e99aSchristosExecute
105a173e99aSchristos.Ar utility
106a173e99aSchristosfor each input line, replacing one or more occurrences of
107a173e99aSchristos.Ar replstr
108a173e99aSchristosin up to
109a173e99aSchristos.Ar replacements
110a173e99aSchristos(or 5 if no
111a173e99aSchristos.Fl R
112a173e99aSchristosflag is specified) arguments to
113a173e99aSchristos.Ar utility
114a173e99aSchristoswith the entire line of input.
115a173e99aSchristosThe resulting arguments, after replacement is done, will not be allowed to grow
116a173e99aSchristosbeyond
117a173e99aSchristos.Ar replsize
118a173e99aSchristos(or 255 if no
119a173e99aSchristos.Fl S
120a173e99aSchristosflag is specified)
121a173e99aSchristosbytes; this is implemented by concatenating as much of the argument
122a173e99aSchristoscontaining
123a173e99aSchristos.Ar replstr
124a173e99aSchristosas possible, to the constructed arguments to
125a173e99aSchristos.Ar utility ,
126a173e99aSchristosup to
127a173e99aSchristos.Ar replsize
128a173e99aSchristosbytes.
129a173e99aSchristosThe size limit does not apply to arguments to
130a173e99aSchristos.Ar utility
131a173e99aSchristoswhich do not contain
132a173e99aSchristos.Ar replstr ,
133a173e99aSchristosand furthermore, no replacement will be done on
134a173e99aSchristos.Ar utility
135a173e99aSchristositself.
136a173e99aSchristosImplies
137a173e99aSchristos.Fl x .
138a173e99aSchristos.It Fl J Ar replstr
139a173e99aSchristosIf this option is specified,
140a173e99aSchristos.Nm
141a173e99aSchristoswill use the data read from standard input to replace the first occurrence of
142a173e99aSchristos.Ar replstr
143a173e99aSchristosinstead of appending that data after all other arguments.
144a173e99aSchristosThis option will not affect how many arguments will be read from input
145a173e99aSchristos.Pq Fl n ,
146a173e99aSchristosor the size of the command(s)
147a173e99aSchristos.Nm
148a173e99aSchristoswill generate
149a173e99aSchristos.Pq Fl s .
150a173e99aSchristosThe option just moves where those arguments will be placed in the command(s)
151a173e99aSchristosthat are executed.
152a173e99aSchristosThe
153a173e99aSchristos.Ar replstr
154a173e99aSchristosmust show up as a distinct
155a173e99aSchristos.Ar argument
156a173e99aSchristosto
157a173e99aSchristos.Nm .
158a173e99aSchristosIt will not be recognized if, for instance, it is in the middle of a
159a173e99aSchristosquoted string.
160a173e99aSchristosFurthermore, only the first occurrence of the
161a173e99aSchristos.Ar replstr
162a173e99aSchristoswill be replaced.
163a173e99aSchristosFor example, the following command will copy the list of files and
164a173e99aSchristosdirectories which start with an uppercase letter in the current
165a173e99aSchristosdirectory to
166a173e99aSchristos.Pa destdir :
167a173e99aSchristos.Pp
168a173e99aSchristos.Dl /bin/ls -1d [A-Z]* | xargs -J % cp -rp % destdir
169a173e99aSchristos.It Fl L Ar number
170a173e99aSchristosCall
171a173e99aSchristos.Ar utility
172a173e99aSchristosfor every
173a173e99aSchristos.Ar number
174a173e99aSchristoslines read.
175a173e99aSchristosIf EOF is reached and fewer lines have been read than
176a173e99aSchristos.Ar number
177a173e99aSchristosthen
178a173e99aSchristos.Ar utility
179a173e99aSchristoswill be called with the available lines.
1802ab235c8Sjtc.It Fl n Ar number
18161f28255ScgdSet the maximum number of arguments taken from standard input for each
182a173e99aSchristosinvocation of
183a173e99aSchristos.Ar utility .
18461f28255ScgdAn invocation of
1852ab235c8Sjtc.Ar utility
18661f28255Scgdwill use less than
1872ab235c8Sjtc.Ar number
18861f28255Scgdstandard input arguments if the number of bytes accumulated (see the
1892ab235c8Sjtc.Fl s
19061f28255Scgdoption) exceeds the specified
1912ab235c8Sjtc.Ar size
19261f28255Scgdor there are fewer than
1932ab235c8Sjtc.Ar number
19461f28255Scgdarguments remaining for the last invocation of
1952ab235c8Sjtc.Ar utility .
19661f28255ScgdThe current default value for
1972ab235c8Sjtc.Ar number
19861f28255Scgdis 5000.
199a173e99aSchristos.It Fl o
200a173e99aSchristosReopen stdin as
2011a2a0445Skleink.Pa /dev/tty
202a173e99aSchristosin the child process before executing the command.
203a173e99aSchristosThis is useful if you want
204a173e99aSchristos.Nm
205a173e99aSchristosto run an interactive application.
206a173e99aSchristos.It Fl P Ar maxprocs
207a173e99aSchristosParallel mode: run at most
208a173e99aSchristos.Ar maxprocs
209a173e99aSchristosinvocations of
210a173e99aSchristos.Ar utility
211a173e99aSchristosat once.
212a173e99aSchristos.It Fl p
213a173e99aSchristosEcho each command to be executed and ask the user whether it should be
214a173e99aSchristosexecuted.
215a173e99aSchristosAn affirmative response,
216a173e99aSchristos.Ql y
217a173e99aSchristosin the POSIX locale,
218a173e99aSchristoscauses the command to be executed, any other response causes it to be
2191a2a0445Skleinkskipped.
220a173e99aSchristosNo commands are executed if the process is not attached to a terminal.
22190cb07e9Schristos.It Fl r
22290cb07e9SchristosCompatibility with GNU
22390cb07e9Schristos.Nm .
22490cb07e9SchristosThe GNU version of
22590cb07e9Schristos.Nm
22690cb07e9Schristosruns the
22790cb07e9Schristos.Ar utility
22890cb07e9Schristosargument at least once, even if
22990cb07e9Schristos.Nm
23090cb07e9Schristosinput is empty, and it supports a
23190cb07e9Schristos.Fl r
23290cb07e9Schristosoption to inhibit this behavior.
23390cb07e9SchristosThe
2348011e5f2Swiz.Nx
23590cb07e9Schristosversion of
23690cb07e9Schristos.Nm
23790cb07e9Schristosdoes not run the
23890cb07e9Schristos.Ar utility
23990cb07e9Schristosargument on empty input, but it supports the
24090cb07e9Schristos.Fl r
24190cb07e9Schristosoption for command-line compatibility with GNU
2428011e5f2Swiz.Nm ;
24390cb07e9Schristosbut the
24490cb07e9Schristos.Fl r
24590cb07e9Schristosoption does nothing in the
2468011e5f2Swiz.Nx
24790cb07e9Schristosversion of
24890cb07e9Schristos.Nm .
249a173e99aSchristos.It Fl R Ar replacements
250a173e99aSchristosSpecify the maximum number of arguments that
251a173e99aSchristos.Fl I
252a173e99aSchristoswill do replacement in.
253a173e99aSchristosIf
254a173e99aSchristos.Ar replacements
255a173e99aSchristosis negative, the number of arguments in which to replace is unbounded.
256a173e99aSchristos.It Fl S Ar replsize
257a173e99aSchristosSpecify the amount of space (in bytes) that
258a173e99aSchristos.Fl I
259a173e99aSchristoscan use for replacements.
260a173e99aSchristosThe default for
261a173e99aSchristos.Ar replsize
262a173e99aSchristosis 255.
2632ab235c8Sjtc.It Fl s Ar size
26461f28255ScgdSet the maximum number of bytes for the command line length provided to
2652ab235c8Sjtc.Ar utility .
266a173e99aSchristosThe sum of the length of the utility name, the arguments passed to
2672ab235c8Sjtc.Ar utility
2685bbd8ba4Sjtc(including
2695bbd8ba4Sjtc.Dv NULL
270a173e99aSchristosterminators) and the current environment will be less than or equal to
271a173e99aSchristosthis number.
27261f28255ScgdThe current default value for
2732ab235c8Sjtc.Ar size
2742ab235c8Sjtcis
2752ab235c8Sjtc.Dv ARG_MAX
2763ff6bc9fSenami- 4096.
2772ab235c8Sjtc.It Fl t
27861f28255ScgdEcho the command to be executed to standard error immediately before it
27961f28255Scgdis executed.
2802ab235c8Sjtc.It Fl x
28161f28255ScgdForce
2824182cf29Slukem.Nm
28361f28255Scgdto terminate immediately if a command line containing
2842ab235c8Sjtc.Ar number
28561f28255Scgdarguments will not fit in the specified (or default) command line length.
2862ab235c8Sjtc.El
2872ab235c8Sjtc.Pp
288a173e99aSchristosIf
2892ab235c8Sjtc.Ar utility
290a173e99aSchristosis omitted,
2912ab235c8Sjtc.Xr echo 1
29261f28255Scgdis used.
2932ab235c8Sjtc.Pp
29461f28255ScgdUndefined behavior may occur if
2952ab235c8Sjtc.Ar utility
29661f28255Scgdreads from the standard input.
2975bbd8ba4Sjtc.Pp
2985bbd8ba4SjtcThe
2994182cf29Slukem.Nm
3005bbd8ba4Sjtcutility exits immediately (without processing any further input) if a
3015bbd8ba4Sjtccommand line cannot be assembled,
3025bbd8ba4Sjtc.Ar utility
303a173e99aSchristoscannot be invoked, an invocation of
304a173e99aSchristos.Ar utility
305a173e99aSchristosis terminated by a signal,
306a173e99aSchristosor an invocation of
307a173e99aSchristos.Ar utility
308a173e99aSchristosexits with a value of 255.
3098011e5f2Swiz.Sh FILES
3108011e5f2Swiz.Bl -tag -width /dev/tty -compact
3118011e5f2Swiz.It Pa /dev/tty
3128011e5f2Swizused to read responses in prompt mode
3138011e5f2Swiz.El
3144918722aSkleink.Sh EXIT STATUS
3154182cf29Slukem.Nm
3162ab235c8Sjtcexits with one of the following values:
3172ab235c8Sjtc.Bl -tag -width Ds -compact
3182ab235c8Sjtc.It 0
3192ab235c8SjtcAll invocations of
3202ab235c8Sjtc.Ar utility
3212ab235c8Sjtcreturned a zero exit status.
3222ab235c8Sjtc.It 123
3232ab235c8SjtcOne or more invocations of
3242ab235c8Sjtc.Ar utility
3252ab235c8Sjtcreturned a nonzero exit status.
3262ab235c8Sjtc.It 124
32761f28255ScgdThe
3282ab235c8Sjtc.Ar utility
3292ab235c8Sjtcexited with a 255 exit status.
3302ab235c8Sjtc.It 125
3312ab235c8SjtcThe
3322ab235c8Sjtc.Ar utility
3332ab235c8Sjtcwas killed or stopped by a signal.
3342ab235c8Sjtc.It 126
3352ab235c8SjtcThe
3362ab235c8Sjtc.Ar utility
3372ab235c8Sjtcwas found but could not be invoked.
3382ab235c8Sjtc.It 127
3392ab235c8SjtcThe
3402ab235c8Sjtc.Ar utility
3412ab235c8Sjtccould not be found.
3422ab235c8Sjtc.It 1
34370a4f3c1SjtcSome other error occurred.
3442ab235c8Sjtc.El
3455bbd8ba4Sjtc.Sh SEE ALSO
3462ab235c8Sjtc.Xr echo 1 ,
347a173e99aSchristos.Xr find 1 ,
348a173e99aSchristos.Xr execvp 3
3492ab235c8Sjtc.Sh STANDARDS
3505bbd8ba4SjtcThe
3514182cf29Slukem.Nm
3525bbd8ba4Sjtcutility is expected to be
3532ab235c8Sjtc.St -p1003.2
3542ab235c8Sjtccompliant.
355a173e99aSchristosThe
3568a3535d6Swiz.Fl J , o , P , R ,
357a173e99aSchristosand
358a173e99aSchristos.Fl S
359a173e99aSchristosoptions are non-standard
360a173e99aSchristos.Fx
361a173e99aSchristosextensions which may not be available on other operating systems.
3622ab235c8Sjtc.Sh HISTORY
363a54d8079SlukemThe
364a173e99aSchristos.Nm
365a173e99aSchristosutility appeared in PWB UNIX 1.0.
366a54d8079SlukemIt made its first BSD appearance in the 4.3 Reno release.
367a54d8079Slukem.Pp
368a54d8079SlukemThe meaning of 123, 124, and 125 exit values and the
369a54d8079Slukem.Fl 0
370a54d8079Slukemoption were taken from GNU xargs.
371a173e99aSchristos.Sh BUGS
372a173e99aSchristosIf
373a173e99aSchristos.Ar utility
374a173e99aSchristosattempts to invoke another command such that the number of arguments or the
375a173e99aSchristossize of the environment is increased, it risks
376a173e99aSchristos.Xr execvp 3
377a173e99aSchristosfailing with
378a173e99aSchristos.Er E2BIG .
379a173e99aSchristos.Pp
380a173e99aSchristosThe
381a173e99aSchristos.Nm
382a173e99aSchristosutility does not take multibyte characters into account when performing
383a173e99aSchristosstring comparisons for the
384a173e99aSchristos.Fl I
385a173e99aSchristosand
386a173e99aSchristos.Fl J
387a173e99aSchristosoptions, which may lead to incorrect results in some locales.
388