# Xargs

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Xargs
> Markdown URL: https://mediated.wiki/source/Xargs.md
> Source: https://en.wikipedia.org/wiki/Xargs
> Source revision: 1336333749
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Standard UNIX utility

xargs Developers Various open-source and commercial developers Operating system Unix, Unix-like, Plan 9, IBM i Platform Cross-platform Type Command

**xargs** (short for "extended arguments")[1] is a [command](/source/Command_(computing)) on [Unix](/source/Unix) and most [Unix-like](/source/Unix-like) [operating systems](/source/Operating_system) used to build and execute commands from [standard input](/source/Standard_streams). It converts input from standard input into arguments to a command.

Some commands such as [grep](/source/Grep) and [awk](/source/Awk) can take input either as command-line arguments or from the standard input. However, others such as [cp](/source/Cp_(Unix)) and [echo](/source/Echo_(command)) can only take input as arguments, which is why **xargs** is necessary.

A port of an older version of GNU xargs is available for [Microsoft Windows](/source/Microsoft_Windows) as part of the [UnxUtils](/source/UnxUtils) collection of [native](/source/Native_(computing)) [Win32](/source/Windows_API) [ports](/source/Porting) of common GNU Unix-like utilities.[2] A ground-up rewrite named wargs is part of the open-source TextTools[3] project. The xargs command has also been ported to the [IBM i](/source/IBM_i) operating system.[4]

## Examples

One use case of the **xargs** command is to remove a list of files using the [rm](/source/Rm_(Unix)) command. [POSIX](/source/POSIX) systems have an ARG_MAX for the maximum total length of the command line,[5][6] 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): rm /path/* or rm $(find /path -type f). (The latter invocation is incorrect, as it may expand [globs](/source/Glob_(programming)) in the output.)

This can be rewritten using the xargs command to break the list of arguments into sublists small enough to be acceptable:

$ find /path -type f -print | xargs rm

In the above example, the [find utility](/source/Find_(Unix)) feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist.

Some implementations of **xargs** can also be used to parallelize operations with the -P maxprocs 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 --output file 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.

$ find /path -name '*.foo' | xargs -P 24 -I '{}' /cpu/bound/process '{}' -o '{}'.out

**xargs** often covers the same functionality as the *command substitution* feature of many [shells](/source/Unix_shell), denoted by the [backquote](/source/Backtick#Use_in_programming) notation (`...` or $(...)). **xargs** is also a good companion for commands that output long lists of files such as [find](/source/Find_(Unix)), [locate](/source/GNU_locate) and [grep](/source/Grep), but only if one uses -0 (or equivalently --null), since xargs without -0 deals badly with file names containing ', " and space. [GNU Parallel](/source/Parallel_(software)) is a similar tool that offers better compatibility with [find](/source/Find_(Unix)), [locate](/source/GNU_locate) and [grep](/source/Grep) when file names may contain ', ", and space (newline still requires -0).

## Placement of arguments

### -I option: single argument

The **xargs** command offers options to insert the listed arguments at some position other than the end of the command line. The -I option to **xargs** takes a string that will be replaced with the supplied input before the command is executed. A common choice is %.

$ mkdir ~/backups
$ find /path -type f -name '*~' -print0 | xargs -0 -I % cp -a % ~/backups

The string to replace may appear multiple times in the command part. Using -I at all limits the number of lines used each time to one.

### Shell trick: any number

Another way to achieve a similar effect is to use a shell as the launched command, and deal with the complexity in that shell, for example:

$ mkdir ~/backups
$ find /path -type f -name '*~' -print0 | xargs -0 sh -c 'for filename; do cp -a "$filename" ~/backups; done' sh

The word sh at the end of the line is for the [POSIX shell](/source/POSIX_shell) sh -c to fill in for $0, the "executable name" part of the positional parameters (argv). If it weren't present, the name of the first matched file would be instead assigned to $0 and the file wouldn't be copied to ~/backups. One can also use any other word to fill in that blank, my-xargs-script for example.

Since cp accepts multiple files at once, one can also simply do the following:

$ find /path -type f -name '*~' -print0 | xargs -0 sh -c 'if [ $# -gt 0 ]; then cp -a "$@" ~/backup; fi' sh

This script runs cp with all the files given to it when there are any arguments passed. Doing so is more efficient since only one invocation of cp is done for each invocation of sh.

## Separator problem

Many Unix utilities are line-oriented. These may work with xargs as long as the lines do not contain ', ", or a space. Some of the Unix utilities can use [NUL](/source/Null_character) as record separator (e.g. [Perl](/source/Perl) (requires -0 and \0 instead of \n), [locate](/source/Locate_(Unix)) (requires using -0), [find](/source/Find_(Unix)) (requires using -print0), [grep](/source/Grep) (requires -z or -Z), [sort](/source/Sort_(Unix)) (requires using -z)). Using -0 for xargs deals with the problem, but many Unix utilities cannot use NUL as separator (e.g. [head](/source/Head_(Unix)), [tail](/source/Tail_(Unix)), [ls](/source/Ls), [echo](/source/Echo_(command)), [sed](/source/Sed), [tar](/source/Tar_(file_format)) -v, [wc](/source/Wc_(Unix)), [which](/source/Which_(command))).

But often people forget this and assume xargs is also line-oriented, which is **not** the case (per default xargs separates on newlines **and** blanks within lines, substrings with blanks must be single- or double-quoted).

The separator problem is illustrated here:

# 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

Running the above will cause important_file to be removed but will remove neither the directory called 12" records, nor the file called not important_file.

The proper fix is to use the GNU-specific -print0 option, but tail (and other tools) do not support NUL-terminated strings:

# use the same preparation commands as above
find . -name not\* -print0 | xargs -0 rm
find \! -name . -type d -print0 | xargs -0 rmdir

When using the -print0 option, entries are separated by a null character instead of an end-of-line. This is equivalent to the more verbose command:find . -name not\* | tr \\n \\0 | xargs -0 rm or shorter, by switching xargs to (non-POSIX) **line-oriented mode** with the -d (delimiter) option: find . -name not\* | xargs -d '\n' rm

but in general using -0 with -print0 should be preferred, since newlines in filenames are still a problem.

GNU [parallel](/source/GNU_parallel) is an alternative to xargs that is designed to have the same options, but is line-oriented. Thus, using GNU Parallel instead, the above would work as expected.[7]

For Unix environments where xargs does not support the -0 nor the -d option (e.g. Solaris, AIX), the POSIX standard states that one can simply backslash-escape every character:find . -name not\* | sed 's/\(.\)/\\\1/g' | xargs rm.[8] Alternatively, one can avoid using xargs at all, either by using GNU parallel or using the -exec ... + functionality of find.

## Operating on a subset of arguments at a time

One might be dealing with commands that can only accept one or maybe two arguments at a time. For example, the diff command operates on two files at a time. The -n option to xargs specifies how many arguments at a time to supply to the given command. The command will be invoked repeatedly until all input is exhausted. Note that on the last invocation one might get fewer than the desired number of arguments if there is insufficient input. Use xargs to break up the input into two arguments per line:

$ echo {0..9} | xargs -n 2
0 1
2 3
4 5
6 7
8 9

In addition to running based on a specified number of arguments at a time, one can also invoke a command for each line of input with the -L 1 option. One can use an arbitrary number of lines at a time, but one is most common. Here is how one might diff every git commit against its parent.[9]

$ git log --format="%H %P" | xargs -L 1 git diff

## Encoding problem

The argument separator processing of xargs is not the only problem with using the xargs program in its default mode. Most Unix tools which are often used to manipulate filenames (for example sed, basename, sort, etc.) are text processing tools. However, Unix path names are not really text. Consider a path name /aaa/bbb/ccc. The /aaa directory and its bbb subdirectory can in general be created by different users with different environments. That means these users could have a different locale setup, and that means that aaa and bbb do not even necessarily have to have the same character encoding. For example, aaa could be in UTF-8 and bbb in Shift JIS. As a result, an absolute path name in a Unix system may not be correctly processable as text under a single character encoding. Tools which rely on their input being text may fail on such strings.

One workaround for this problem is to run such tools in the C locale, which essentially processes the bytes of the input as-is. However, this will change the behavior of the tools in ways the user may not expect (for example, some of the user's expectations about case-folding behavior may not be met).

## References

1. **[^](#cite_ref-1)** ["The Unix Acronym List: The Complete List"](http://www.roesler-ac.de/wolfram/acro/all.htm). *www.roesler-ac.de*. [Archived](https://web.archive.org/web/20120716205342/http://www.roesler-ac.de/wolfram/acro/all.htm) from the original on 2012-07-16. Retrieved 2020-04-12.

1. **[^](#cite_ref-2)** ["Native Win32 ports of some GNU utilities"](https://unxutils.sourceforge.net/). *unxutils.sourceforge.net*. [Archived](https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/) from the original on 2006-02-09. Retrieved 2025-08-11.

1. **[^](#cite_ref-3)** ["Text processing tools for Windows"](https://github.com/idigdoug/TextTools). [Archived](https://web.archive.org/web/20220322234544/https://github.com/idigdoug/TextTools) from the original on 2022-03-22. Retrieved 2022-03-22.

1. **[^](#cite_ref-4)** [IBM](/source/IBM). ["IBM System i Version 7.2 Programming Qshell"](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc) (PDF). [Archived](https://web.archive.org/web/20200918130823/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc) (PDF) from the original on 2020-09-18. Retrieved 2020-09-05.

1. **[^](#cite_ref-5)** ["GNU Core Utilities Frequently Asked Questions"](https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long). [Archived](https://web.archive.org/web/20151123211008/http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long) from the original on November 23, 2015. Retrieved December 7, 2015.

1. **[^](#cite_ref-6)** ["The maximum length of arguments for a new process"](https://www.in-ulm.de/~mascheck/various/argmax/). *www.in-ulm.de*.

1. **[^](#cite_ref-7)** [Differences Between xargs and GNU Parallel](https://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel) [Archived](https://web.archive.org/web/20160729114848/http://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel) 2016-07-29 at the [Wayback Machine](/source/Wayback_Machine). [GNU.org](http://www.gnu.org) [Archived](https://web.archive.org/web/20110222101726/http://www.gnu.org/) 2011-02-22 at the [Wayback Machine](/source/Wayback_Machine). Accessed February 2012.

1. **[^](#cite_ref-8)** [xargs](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/xargs.html) – Shell and Utilities Reference, [The Single UNIX Specification](/source/Single_Unix_Specification), Version 5 from [The Open Group](/source/The_Open_Group)

1. **[^](#cite_ref-9)** Cosmin Stejerean. ["Things you (probably) didn't know about xargs"](http://offbytwo.com/2011/06/26/things-you-didnt-know-about-xargs.html). [Archived](https://web.archive.org/web/20151126011055/http://offbytwo.com/2011/06/26/things-you-didnt-know-about-xargs.html) from the original on November 26, 2015. Retrieved December 7, 2015.

## External links

The Wikibook *[Guide to Unix](https://en.wikibooks.org/wiki/Guide_to_Unix)* has a page on the topic of: ***[Commands](https://en.wikibooks.org/wiki/Guide_to_Unix/Commands)***

- [xargs](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/xargs.html): construct argument lists and invoke utility – Shell and Utilities Reference, [The Single UNIX Specification](/source/Single_Unix_Specification), Version 5 from [The Open Group](/source/The_Open_Group)

### Manual pages

- [xargs(1)](https://www.gnu.org/software/findutils/manual/html_node/find_html/Invoking-xargs.html) – [GNU](/source/GNU) [Findutils](/source/Findutils) reference

- [xargs(1)](https://www.freebsd.org/cgi/man.cgi?query=xargs&sektion=1): construct argument list(s) and execute utility – [FreeBSD](/source/FreeBSD) General Commands [Manual](/source/Man_page)

- [xargs(1)](https://man.netbsd.org/xargs.1): construct argument list(s) and execute utility – [NetBSD](/source/NetBSD) General Commands [Manual](/source/Man_page)

- [xargs(1)](https://man.openbsd.org/xargs.1): construct argument list(s) and execute utility – [OpenBSD](/source/OpenBSD) General Commands [Manual](/source/Man_page)

- [xargs(1)](https://docs.oracle.com/cd/E88353_01/html/E37839/xargs-1.html): construct argument lists and invoke utility – [Solaris 11.4](/source/Solaris_(operating_system)) User Commands Reference [Manual](/source/Man_page)

v t e Unix command-line utilities and shell builtins File system cat chattr chmod chown chgrp cksum cmp cp dd du df file fuser ln ls mkdir mv pax pwd rm rmdir split tee touch type umask Processes at bg crontab fg kill nice ps time User environment env exit logname mesg talk tput uname who write Text processing awk basename comm csplit cut diff dirname ed ex fold head iconv join m4 more nl paste patch printf read sed sort strings tail tr troff uniq vi wc xargs Shell builtins alias cd echo test unset wait Searching find grep Documentation man Software development ar ctags lex make nm strip yacc Miscellaneous bc cal dc expr lp od sleep true and false Categories Standard Unix programs Unix SUS2008 utilities List

---
Adapted from the Wikipedia article [Xargs](https://en.wikipedia.org/wiki/Xargs) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Xargs?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
