]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/xargs/xargs.1
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / usr.bin / xargs / xargs.1
1 .\" Copyright (c) 1990, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" John B. Roll Jr. and the Institute of Electrical and Electronics
6 .\" Engineers, Inc.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)xargs.1     8.1 (Berkeley) 6/6/93
33 .\" $FreeBSD$
34 .\" $xMach: xargs.1,v 1.2 2002/02/23 05:23:37 tim Exp $
35 .\"
36 .Dd September 21, 2020
37 .Dt XARGS 1
38 .Os
39 .Sh NAME
40 .Nm xargs
41 .Nd "construct argument list(s) and execute utility"
42 .Sh SYNOPSIS
43 .Nm
44 .Op Fl 0oprt
45 .Op Fl E Ar eofstr
46 .Oo
47 .Fl I Ar replstr
48 .Op Fl R Ar replacements
49 .Op Fl S Ar replsize
50 .Oc
51 .Op Fl J Ar replstr
52 .Op Fl L Ar number
53 .Oo
54 .Fl n Ar number
55 .Op Fl x
56 .Oc
57 .Op Fl P Ar maxprocs
58 .Op Fl s Ar size
59 .Op Ar utility Op Ar argument ...
60 .Sh DESCRIPTION
61 The
62 .Nm
63 utility reads space, tab, newline and end-of-file delimited strings
64 from the standard input and executes
65 .Ar utility
66 with the strings as
67 arguments.
68 .Pp
69 Any arguments specified on the command line are given to
70 .Ar utility
71 upon each invocation, followed by some number of the arguments read
72 from the standard input of
73 .Nm .
74 This is repeated until standard input is exhausted.
75 .Pp
76 Spaces, tabs and newlines may be embedded in arguments using single
77 (``\ '\ '')
78 or double (``"'') quotes or backslashes (``\e'').
79 Single quotes escape all non-single quote characters, excluding newlines,
80 up to the matching single quote.
81 Double quotes escape all non-double quote characters, excluding newlines,
82 up to the matching double quote.
83 Any single character, including newlines, may be escaped by a backslash.
84 .Pp
85 The options are as follows:
86 .Bl -tag -width indent
87 .It Fl 0 , Fl -null
88 Change
89 .Nm
90 to expect NUL
91 (``\\0'')
92 characters as separators, instead of spaces and newlines.
93 This is expected to be used in concert with the
94 .Fl print0
95 function in
96 .Xr find 1 .
97 .It Fl E Ar eofstr
98 Use
99 .Ar eofstr
100 as a logical EOF marker.
101 .It Fl I Ar replstr
102 Execute
103 .Ar utility
104 for each input line, replacing one or more occurrences of
105 .Ar replstr
106 in up to
107 .Ar replacements
108 (or 5 if no
109 .Fl R
110 flag is specified) arguments to
111 .Ar utility
112 with the entire line of input.
113 The resulting arguments, after replacement is done, will not be allowed to grow
114 beyond
115 .Ar replsize
116 (or 255 if no
117 .Fl S
118 flag is specified)
119 bytes; this is implemented by concatenating as much of the argument
120 containing
121 .Ar replstr
122 as possible, to the constructed arguments to
123 .Ar utility ,
124 up to
125 .Ar replsize
126 bytes.
127 The size limit does not apply to arguments to
128 .Ar utility
129 which do not contain
130 .Ar replstr ,
131 and furthermore, no replacement will be done on
132 .Ar utility
133 itself.
134 Implies
135 .Fl x .
136 .It Fl J Ar replstr
137 If this option is specified,
138 .Nm
139 will use the data read from standard input to replace the first occurrence of
140 .Ar replstr
141 instead of appending that data after all other arguments.
142 This option will not affect how many arguments will be read from input
143 .Pq Fl n ,
144 or the size of the command(s)
145 .Nm
146 will generate
147 .Pq Fl s .
148 The option just moves where those arguments will be placed in the command(s)
149 that are executed.
150 The
151 .Ar replstr
152 must show up as a distinct
153 .Ar argument
154 to
155 .Nm .
156 It will not be recognized if, for instance, it is in the middle of a
157 quoted string.
158 Furthermore, only the first occurrence of the
159 .Ar replstr
160 will be replaced.
161 For example, the following command will copy the list of files and
162 directories which start with an uppercase letter in the current
163 directory to
164 .Pa destdir :
165 .Pp
166 .Dl /bin/ls -1d [A-Z]* | xargs -J % cp -Rp % destdir
167 .It Fl L Ar number
168 Call
169 .Ar utility
170 for every
171 .Ar number
172 lines read.
173 If EOF is reached and fewer lines have been read than
174 .Ar number
175 then
176 .Ar utility
177 will be called with the available lines.
178 .It Fl n Ar number , Fl -max-args= Ns Ar number
179 Set the maximum number of arguments taken from standard input for each
180 invocation of
181 .Ar utility .
182 An invocation of
183 .Ar utility
184 will use less than
185 .Ar number
186 standard input arguments if the number of bytes accumulated (see the
187 .Fl s
188 option) exceeds the specified
189 .Ar size
190 or there are fewer than
191 .Ar number
192 arguments remaining for the last invocation of
193 .Ar utility .
194 The current default value for
195 .Ar number
196 is 5000.
197 .It Fl o
198 Reopen stdin as
199 .Pa /dev/tty
200 in the child process before executing the command.
201 This is useful if you want
202 .Nm
203 to run an interactive application.
204 .It Fl P Ar maxprocs , Fl -max-procs= Ns Ar maxprocs
205 Parallel mode: run at most
206 .Ar maxprocs
207 invocations of
208 .Ar utility
209 at once.
210 If
211 .Ar maxprocs
212 is set to 0,
213 .Nm
214 will run as many processes as possible.
215 .It Fl p , Fl -interactive
216 Echo each command to be executed and ask the user whether it should be
217 executed.
218 An affirmative response,
219 .Ql y
220 in the POSIX locale,
221 causes the command to be executed, any other response causes it to be
222 skipped.
223 No commands are executed if the process is not attached to a terminal.
224 .It Fl r , Fl -no-run-if-empty
225 Compatibility with GNU
226 .Nm .
227 The GNU version of
228 .Nm
229 runs the
230 .Ar utility
231 argument at least once, even if
232 .Nm
233 input is empty, and it supports a
234 .Fl r
235 option to inhibit this behavior.
236 The
237 .Fx
238 version of
239 .Nm
240 does not run the
241 .Ar utility
242 argument on empty input, but it supports the
243 .Fl r
244 option for command-line compatibility with GNU
245 .Nm ,
246 but the
247 .Fl r
248 option does nothing in the
249 .Fx
250 version of
251 .Nm .
252 .It Fl R Ar replacements
253 Specify the maximum number of arguments that
254 .Fl I
255 will do replacement in.
256 If
257 .Ar replacements
258 is negative, the number of arguments in which to replace is unbounded.
259 .It Fl S Ar replsize
260 Specify the amount of space (in bytes) that
261 .Fl I
262 can use for replacements.
263 The default for
264 .Ar replsize
265 is 255.
266 .It Fl s Ar size , Fl -max-chars= Ns Ar size
267 Set the maximum number of bytes for the command line length provided to
268 .Ar utility .
269 The sum of the length of the utility name, the arguments passed to
270 .Ar utility
271 (including
272 .Dv NULL
273 terminators) and the current environment will be less than or equal to
274 this number.
275 The current default value for
276 .Ar size
277 is
278 .Dv ARG_MAX
279 - 4096.
280 .It Fl t , Fl -verbose
281 Echo the command to be executed to standard error immediately before it
282 is executed.
283 .It Fl x , Fl -exit
284 Force
285 .Nm
286 to terminate immediately if a command line containing
287 .Ar number
288 arguments will not fit in the specified (or default) command line length.
289 .El
290 .Pp
291 If
292 .Ar utility
293 is omitted,
294 .Xr echo 1
295 is used.
296 .Pp
297 Undefined behavior may occur if
298 .Ar utility
299 reads from the standard input.
300 .Pp
301 If a command line cannot be assembled, or
302 cannot be invoked, or if an invocation of
303 .Ar utility
304 is terminated by a signal,
305 or an invocation of
306 .Ar utility
307 exits with a value of 255, the
308 .Nm
309 utility stops processing input and exits after all invocations of
310 .Ar utility
311 finish processing.
312 .Sh EXIT STATUS
313 The
314 .Nm
315 utility exits with a value of 0 if no error occurs.
316 If
317 .Ar utility
318 cannot be found,
319 .Nm
320 exits with a value of 127, otherwise if
321 .Ar utility
322 cannot be executed,
323 .Nm
324 exits with a value of 126.
325 If any other error occurs,
326 .Nm
327 exits with a value of 1.
328 .Sh EXAMPLES
329 Create a 3x3 matrix with numbers from 1 to 9.
330 Every
331 .Xr echo 1
332 instance receives three lines as arguments:
333 .Bd -literal -offset indent
334 $ seq 1 9 | xargs -L3 echo
335 1 2 3
336 4 5 6
337 7 8 9
338 .Ed
339 .Pp
340 Duplicate every line from standard input:
341 .Bd -literal -offset indent
342 $ echo -e "one\\ntwo\\nthree" | xargs -I % echo % %
343 one one
344 two two
345 three three
346 .Ed
347 .Pp
348 Execute at most 2 concurrent instances of
349 .Xr find 1
350 every one of them using one of the directories from the standard input:
351 .Bd -literal -offset indent
352 echo -e "/usr/ports\\n/etc\\n/usr/local" | xargs -J % -P2 -n1 find % -name file
353 .Ed
354 .Sh SEE ALSO
355 .Xr echo 1 ,
356 .Xr find 1 ,
357 .Xr execvp 3
358 .Sh STANDARDS
359 The
360 .Nm
361 utility is expected to be
362 .St -p1003.2
363 compliant.
364 The
365 .Fl J , o , P , R
366 and
367 .Fl S
368 options are non-standard
369 .Fx
370 extensions which may not be available on other operating systems.
371 .Sh HISTORY
372 The
373 .Nm
374 utility appeared in PWB UNIX.
375 .Sh BUGS
376 If
377 .Ar utility
378 attempts to invoke another command such that the number of arguments or the
379 size of the environment is increased, it risks
380 .Xr execvp 3
381 failing with
382 .Er E2BIG .
383 .Pp
384 The
385 .Nm
386 utility does not take multibyte characters into account when performing
387 string comparisons for the
388 .Fl I
389 and
390 .Fl J
391 options, which may lead to incorrect results in some locales.