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