On Unix-like operating systems, the xargs command builds and executes command lines from standard input. It enables you to run the same command on many files.
This page covers the Linux version of xargs.
Description
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (the default command is echo, located at /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
- Description
- Syntax
- Exit status
- Examples
- Related commands
- Linux commands help
Because Unix file names can contain blanks and newlines, this default behaviour is often problematic; file names containing blanks or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option (that’s a zero, not a capital o), which prevents such problems. When using this option, you need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is find for example, the -print0 option does this for you.
If any invocation of the command exits with a status of 255, xargs stops immediately without reading any further input. An error message is issued on stderr when this happens.
This documentation is specific to the GNU version of xargs, which is commonly distributed with most variants of Linux.
Syntax
xargs [-0prtx] [-E eof-str] [-e[eof-str]] [–eof[=eof-str]] [–null] [-d delimiter] [–delimiter delimiter] [-I replace-str] [-i[replace-str]] [–replace[=replace-str]] [-l[max-lines]] [-L max-lines] [–max-lines[=max-lines]] [-n max-args] [–max-args=max-args] [-s max-chars] [–max-chars=max-chars] [-P max-procs] [–max-procs=max-procs] [–interactive] [–verbose] [–exit] [–no-run-if-empty] [–arg-file=file] [–show-limits] [–version] [–help] [command [initial-arguments]]
Options
Exit status
xargs exits with the following status:
Examples
find /tmp -name core -type f -print | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any file names containing newlines or spaces.
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing file names in such a way that file or directory names containing spaces or newlines are correctly handled.
find /tmp -depth -name core -type f -delete
Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork and exec rm, and we don’t need the extra xargs process).
cut -d: -f1 < /etc/passwd | sort | xargs echo
Uses cut to generate a compact listing of all the users on the system.
xargs sh -c ’emacs “[email protected]” < /dev/tty’ emacs
Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs’ standard input.
Related commands
find — Find files within a directory hierarchy.locate — Search a local database to find files by name.