Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Xargs
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== One use case of the '''xargs''' command is to remove a list of files using the [[rm (Unix)|rm]] command. [[POSIX]] systems have an {{tt|ARG_MAX}} for the maximum total length of the command line,<ref>{{cite web|url=https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long|title=GNU Core Utilities Frequently Asked Questions|access-date=December 7, 2015}}</ref><ref>{{cite web |title=The maximum length of arguments for a new process |url=https://www.in-ulm.de/~mascheck/various/argmax/ |website=www.in-ulm.de}}</ref> so the command may fail with an error message of "Argument list too long" (meaning that the exec system call's limit on the length of a command line was exceeded): <syntaxhighlight lang="bash" inline>rm /path/*</syntaxhighlight> or <syntaxhighlight lang="bash" inline>rm $(find /path -type f)</syntaxhighlight>. (The latter invocation is incorrect, as it may expand [[glob (programming)|globs]] in the output.) This can be rewritten using the <code>xargs</code> command to break the list of arguments into sublists small enough to be acceptable: <syntaxhighlight lang="console"> $ find /path -type f -print | xargs rm </syntaxhighlight> In the above example, the [[Find (Unix)|<code>find</code> utility]] feeds the input of <code>xargs</code> with a long list of file names. <code>xargs</code> then splits this list into sublists and calls <code>rm</code> once for every sublist. Some implementations of '''xargs''' can also be used to parallelize operations with the <code>-P maxprocs</code> argument to specify how many parallel processes should be used to execute the commands over the input argument lists. However, the output streams may not be synchronized. This can be overcome by using an <code>--output file</code> argument where possible, and then combining the results after processing. The following example queues 24 processes and waits on each to finish before launching another. <syntaxhighlight lang="console"> $ find /path -name '*.foo' | xargs -P 24 -I '{}' /cpu/bound/process '{}' -o '{}'.out </syntaxhighlight> '''xargs''' often covers the same functionality as the ''command substitution'' feature of many [[Unix shell|shells]], denoted by the [[Backtick#Use in programming|backquote]] notation (<code>`...`</code> or <code>$(...)</code>). '''xargs''' is also a good companion for commands that output long lists of files such as <code>[[Find (Unix)|find]]</code>, <code>[[GNU locate|locate]]</code> and <code>[[grep]]</code>, but only if one uses <code>-0</code> (or equivalently <code>--null</code>), since <code>xargs</code> without <code>-0</code> deals badly with file names containing <code>'</code>, <code>"</code> and space. [[Parallel (software)|GNU Parallel]] is a similar tool that offers better compatibility with [[Find (Unix)|find]], [[GNU locate|locate]] and [[grep]] when file names may contain <code>'</code>, <code>"</code>, and space (newline still requires <code>-0</code>).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)