1XARGS(1) General Commands Manual XARGS(1)
2
3
4
6 xargs - build and execute command lines from standard input
7
9 xargs [options] [command [initial-arguments]]
10
12 This manual page documents the GNU version of xargs. xargs reads items
13 from the standard input, delimited by blanks (which can be protected
14 with double or single quotes or a backslash) or newlines, and executes
15 the command (default is /bin/echo) one or more times with any initial-
16 arguments followed by items read from standard input. Blank lines on
17 the standard input are ignored.
18
19 The command line for command is built up until it reaches a system-de‐
20 fined limit (unless the -n and -L options are used). The specified
21 command will be invoked as many times as necessary to use up the list
22 of input items. In general, there will be many fewer invocations of
23 command than there were items in the input. This will normally have
24 significant performance benefits. Some commands can usefully be exe‐
25 cuted in parallel too; see the -P option.
26
27 Because Unix filenames can contain blanks and newlines, this default
28 behaviour is often problematic; filenames containing blanks and/or new‐
29 lines are incorrectly processed by xargs. In these situations it is
30 better to use the -0 option, which prevents such problems. When using
31 this option you will need to ensure that the program which produces the
32 input for xargs also uses a null character as a separator. If that
33 program is GNU find for example, the -print0 option does this for you.
34
35 If any invocation of the command exits with a status of 255, xargs will
36 stop immediately without reading any further input. An error message
37 is issued on stderr when this happens.
38
40 -0, --null
41 Input items are terminated by a null character instead of by
42 whitespace, and the quotes and backslash are not special (every
43 character is taken literally). Disables the end of file string,
44 which is treated like any other argument. Useful when input
45 items might contain white space, quote marks, or backslashes.
46 The GNU find -print0 option produces input suitable for this
47 mode.
48
49
50 -a file, --arg-file=file
51 Read items from file instead of standard input. If you use this
52 option, stdin remains unchanged when commands are run. Other‐
53 wise, stdin is redirected from /dev/null.
54
55
56 --delimiter=delim, -d delim
57 Input items are terminated by the specified character. The
58 specified delimiter may be a single character, a C-style charac‐
59 ter escape such as \n, or an octal or hexadecimal escape code.
60 Octal and hexadecimal escape codes are understood as for the
61 printf command. Multibyte characters are not supported. When
62 processing the input, quotes and backslash are not special; ev‐
63 ery character in the input is taken literally. The -d option
64 disables any end-of-file string, which is treated like any other
65 argument. You can use this option when the input consists of
66 simply newline-separated items, although it is almost always
67 better to design your program to use --null where this is possi‐
68 ble.
69
70
71 -E eof-str
72 Set the end of file string to eof-str. If the end of file
73 string occurs as a line of input, the rest of the input is ig‐
74 nored. If neither -E nor -e is used, no end of file string is
75 used.
76
77 -e[eof-str], --eof[=eof-str]
78 This option is a synonym for the -E option. Use -E instead, be‐
79 cause it is POSIX compliant while this option is not. If eof-
80 str is omitted, there is no end of file string. If neither -E
81 nor -e is used, no end of file string is used.
82
83 -I replace-str
84 Replace occurrences of replace-str in the initial-arguments with
85 names read from standard input. Also, unquoted blanks do not
86 terminate input items; instead the separator is the newline
87 character. Implies -x and -L 1.
88
89 -i[replace-str], --replace[=replace-str]
90 This option is a synonym for -Ireplace-str if replace-str is
91 specified. If the replace-str argument is missing, the effect
92 is the same as -I{}. This option is deprecated; use -I instead.
93
94 -L max-lines
95 Use at most max-lines nonblank input lines per command line.
96 Trailing blanks cause an input line to be logically continued on
97 the next input line. Implies -x.
98
99 -l[max-lines], --max-lines[=max-lines]
100 Synonym for the -L option. Unlike -L, the max-lines argument is
101 optional. If max-lines is not specified, it defaults to one.
102 The -l option is deprecated since the POSIX standard specifies
103 -L instead.
104
105 -n max-args, --max-args=max-args
106 Use at most max-args arguments per command line. Fewer than
107 max-args arguments will be used if the size (see the -s option)
108 is exceeded, unless the -x option is given, in which case xargs
109 will exit.
110
111 -P max-procs, --max-procs=max-procs
112 Run up to max-procs processes at a time; the default is 1. If
113 max-procs is 0, xargs will run as many processes as possible at
114 a time. Use the -n option or the -L option with -P; otherwise
115 chances are that only one exec will be done. While xargs is
116 running, you can send its process a SIGUSR1 signal to increase
117 the number of commands to run simultaneously, or a SIGUSR2 to
118 decrease the number. You cannot increase it above an implemen‐
119 tation-defined limit (which is shown with --show-limits). You
120 cannot decrease it below 1. xargs never terminates its com‐
121 mands; when asked to decrease, it merely waits for more than one
122 existing command to terminate before starting another.
123
124 Please note that it is up to the called processes to properly
125 manage parallel access to shared resources. For example, if
126 more than one of them tries to print to stdout, the output will
127 be produced in an indeterminate order (and very likely mixed up)
128 unless the processes collaborate in some way to prevent this.
129 Using some kind of locking scheme is one way to prevent such
130 problems. In general, using a locking scheme will help ensure
131 correct output but reduce performance. If you don't want to
132 tolerate the performance difference, simply arrange for each
133 process to produce a separate output file (or otherwise use sep‐
134 arate resources).
135
136 -o, --open-tty
137 Reopen stdin as /dev/tty in the child process before executing
138 the command. This is useful if you want xargs to run an inter‐
139 active application.
140
141 -p, --interactive
142 Prompt the user about whether to run each command line and read
143 a line from the terminal. Only run the command line if the re‐
144 sponse starts with `y' or `Y'. Implies -t.
145
146 --process-slot-var=name
147 Set the environment variable name to a unique value in each run‐
148 ning child process. Values are reused once child processes ex‐
149 it. This can be used in a rudimentary load distribution scheme,
150 for example.
151
152 -r, --no-run-if-empty
153 If the standard input does not contain any nonblanks, do not run
154 the command. Normally, the command is run once even if there is
155 no input. This option is a GNU extension.
156
157 -s max-chars, --max-chars=max-chars
158 Use at most max-chars characters per command line, including the
159 command and initial-arguments and the terminating nulls at the
160 ends of the argument strings. The largest allowed value is sys‐
161 tem-dependent, and is calculated as the argument length limit
162 for exec, less the size of your environment, less 2048 bytes of
163 headroom. If this value is more than 128KiB, 128Kib is used as
164 the default value; otherwise, the default value is the maximum.
165 1KiB is 1024 bytes. xargs automatically adapts to tighter con‐
166 straints.
167
168 --show-limits
169 Display the limits on the command-line length which are imposed
170 by the operating system, xargs' choice of buffer size and the -s
171 option. Pipe the input from /dev/null (and perhaps specify
172 --no-run-if-empty) if you don't want xargs to do anything.
173
174 -t, --verbose
175 Print the command line on the standard error output before exe‐
176 cuting it.
177
178 -x, --exit
179 Exit if the size (see the -s option) is exceeded.
180
181 --help Print a summary of the options to xargs and exit.
182
183 --version
184 Print the version number of xargs and exit.
185
187 find /tmp -name core -type f -print | xargs /bin/rm -f
188
189 Find files named core in or below the directory /tmp and delete them.
190 Note that this will work incorrectly if there are any filenames con‐
191 taining newlines or spaces.
192
193 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
194
195 Find files named core in or below the directory /tmp and delete them,
196 processing filenames in such a way that file or directory names con‐
197 taining spaces or newlines are correctly handled.
198
199
200 find /tmp -depth -name core -type f -delete
201
202 Find files named core in or below the directory /tmp and delete them,
203 but more efficiently than in the previous example (because we avoid the
204 need to use fork(2) and exec(2) to launch rm and we don't need the ex‐
205 tra xargs process).
206
207
208 cut -d: -f1 < /etc/passwd | sort | xargs echo
209
210 Generates a compact listing of all the users on the system.
211
212
213
214
216 xargs exits with the following status:
217 0 if it succeeds
218 123 if any invocation of the command exited with status 1-125
219 124 if the command exited with status 255
220 125 if the command is killed by a signal
221 126 if the command cannot be run
222 127 if the command is not found
223 1 if some other error occurred.
224
225 Exit codes greater than 128 are used by the shell to indicate that a
226 program died due to a fatal signal.
227
229 As of GNU xargs version 4.2.9, the default behaviour of xargs is not to
230 have a logical end-of-file marker. POSIX (IEEE Std 1003.1, 2004 Edi‐
231 tion) allows this.
232
233 The -l and -i options appear in the 1997 version of the POSIX standard,
234 but do not appear in the 2004 version of the standard. Therefore you
235 should use -L and -I instead, respectively.
236
237 The -o option is an extension to the POSIX standard for better compati‐
238 bility with BSD.
239
240 The POSIX standard allows implementations to have a limit on the size
241 of arguments to the exec functions. This limit could be as low as 4096
242 bytes including the size of the environment. For scripts to be porta‐
243 ble, they must not rely on a larger value. However, I know of no im‐
244 plementation whose actual limit is that small. The --show-limits op‐
245 tion can be used to discover the actual limits in force on the current
246 system.
247
248
249
251 find(1), locate(1), locatedb(5), updatedb(1), fork(2), execvp(3),
252 kill(1), signal(7),
253
254 The full documentation for xargs is maintained as a Texinfo manual.
255 If the info and xargs programs are properly installed at your site, the
256 command info xargs should give you access to the complete manual.
257
258
260 Copyright © 1990-2019 Free Software Foundation, Inc. License GPLv3+:
261 GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
262 This is free software: you are free to change and redistribute it.
263 There is NO WARRANTY, to the extent permitted by law.
264
265
267 The -L option is incompatible with the -I option, but perhaps should
268 not be.
269
270 It is not possible for xargs to be used securely, since there will al‐
271 ways be a time gap between the production of the list of input files
272 and their use in the commands that xargs issues. If other users have
273 access to the system, they can manipulate the filesystem during this
274 time window to force the action of the commands xargs runs to apply to
275 files that you didn't intend. For a more detailed discussion of this
276 and related problems, please refer to the ``Security Considerations''
277 chapter in the findutils Texinfo documentation. The -execdir option of
278 find can often be used as a more secure alternative.
279
280 When you use the -I option, each line read from the input is buffered
281 internally. This means that there is an upper limit on the length of
282 input line that xargs will accept when used with the -I option. To
283 work around this limitation, you can use the -s option to increase the
284 amount of buffer space that xargs uses, and you can also use an extra
285 invocation of xargs to ensure that very long lines do not occur. For
286 example:
287
288 somecommand | xargs -s 50000 echo | xargs -I '{}' -s 100000 rm '{}'
289
290 Here, the first invocation of xargs has no input line length limit be‐
291 cause it doesn't use the -i option. The second invocation of xargs
292 does have such a limit, but we have ensured that the it never encoun‐
293 ters a line which is longer than it can handle. This is not an ideal
294 solution. Instead, the -i option should not impose a line length lim‐
295 it, which is why this discussion appears in the BUGS section. The
296 problem doesn't occur with the output of find(1) because it emits just
297 one filename per line.
298
299 The best way to report a bug is to use the form at https://savan‐
300 nah.gnu.org/bugs/?group=findutils. The reason for this is that you
301 will then be able to track progress in fixing the problem. Other com‐
302 ments about xargs(1) and about the findutils package in general can be
303 sent to the bug-findutils mailing list. To join the list, send email
304 to bug-findutils-request@gnu.org.
305
306
307
308 XARGS(1)