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!
==Separator problem== Many Unix utilities are line-oriented. These may work with <code>xargs</code> as long as the lines do not contain <code>'</code>, <code>"</code>, or a space. Some of the Unix utilities can use [[Null character|NUL]] as record separator (e.g. [[Perl]] (requires <code>-0</code> and <code>\0</code> instead of <code>\n</code>), <code>[[Locate (Unix)|locate]]</code> (requires using <code>-0</code>), <code>[[Find (Unix)|find]]</code> (requires using <code>-print0</code>), <code>[[grep]]</code> (requires <code>-z</code> or <code>-Z</code>), <code>[[Sort (Unix)|sort]]</code> (requires using <code>-z</code>)). Using <code>-0</code> for <code>xargs</code> deals with the problem, but many Unix utilities cannot use NUL as separator (e.g. <code>[[head (Unix)|head]]</code>, <code>[[tail (Unix)|tail]]</code>, <code>[[ls]]</code>, <code>[[echo (command)|echo]]</code>, <code>[[sed]]</code>, <code>[[tar (file format)|tar]] -v</code>, <code>[[wc (Unix)|wc]]</code>, <code>[[which (command)|which]]</code>). But often people forget this and assume <code>xargs</code> is also line-oriented, which is '''not''' the case (per default <code>xargs</code> separates on newlines '''and''' blanks within lines, substrings with blanks must be single- or double-quoted). The separator problem is illustrated here: <syntaxhighlight lang="bash"> # Make some targets to practice on touch important_file touch 'not important_file' mkdir -p '12" records' find . -name not\* | tail -1 | xargs rm find \! -name . -type d | tail -1 | xargs rmdir </syntaxhighlight> Running the above will cause <code>important_file</code> to be removed but will remove neither the directory called <code>12" records</code>, nor the file called <code>not important_file</code>. The proper fix is to use the GNU-specific <code>-print0</code> option, but <code>tail</code> (and other tools) do not support NUL-terminated strings: <syntaxhighlight lang="bash"> # use the same preparation commands as above find . -name not\* -print0 | xargs -0 rm find \! -name . -type d -print0 | xargs -0 rmdir </syntaxhighlight> When using the <code>-print0</code> option, entries are separated by a null character instead of an end-of-line. This is equivalent to the more verbose command:<syntaxhighlight lang="bash" inline>find . -name not\* | tr \\n \\0 | xargs -0 rm</syntaxhighlight> or shorter, by switching <code>xargs</code> to (non-POSIX) '''line-oriented mode''' with the <code>-d</code> (delimiter) option: <syntaxhighlight lang="bash" inline>find . -name not\* | xargs -d '\n' rm</syntaxhighlight> but in general using <code>-0</code> with <code>-print0</code> should be preferred, since newlines in filenames are still a problem. GNU <code>[[GNU parallel|parallel]]</code> is an alternative to <code>xargs</code> that is designed to have the same options, but is line-oriented. Thus, using GNU Parallel instead, the above would work as expected.<ref>[https://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel Differences Between xargs and GNU Parallel]. [http://www.gnu.org GNU.org]. Accessed February 2012.</ref> For Unix environments where <code>xargs</code> does not support the <code>-0</code> nor the {{code|-d}} option (e.g. Solaris, AIX), the POSIX standard states that one can simply backslash-escape every character:<syntaxhighlight lang="bash" inline>find . -name not\* | sed 's/\(.\)/\\\1/g' | xargs rm</syntaxhighlight>.<ref>{{man|1|xargs|SUS}}</ref> Alternatively, one can avoid using xargs at all, either by using GNU parallel or using the {{code|-exec ... +}} functionality of {{code|find}}.
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)