LinuxCommandLibrary

xargs

Execute commands with standard input arguments

TLDR

Run a command using the input data as arguments

$ [arguments_source] | xargs [command]
copy

Run multiple chained commands on the input data
$ [arguments_source] | xargs sh -c "[command1] && [command2] | [command3]"
copy

Gzip all files with .log extension taking advantage of multiple threads (-print0 uses a null character to split file names, and -0 uses it as delimiter)
$ find . -name '*.log' -print0 | xargs [[-0|--null]] [[-P|--max-procs]] [4] [[-n|--max-args]] 1 gzip
copy

Execute the command once per argument
$ [arguments_source] | xargs [[-n|--max-args]] 1 [command]
copy

Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as _) with the input line
$ [arguments_source] | xargs -I _ [command] _ [optional_extra_arguments]
copy

Parallel runs of up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time
$ [arguments_source] | xargs [[-P|--max-procs]] [max-procs] [command]
copy

Prompt user for confirmation before executing command (confirm with y or Y)
$ [arguments_source] | xargs [[-p|--interactive]] [command]
copy

SYNOPSIS

xargs [options] [command [initial-arguments]]

PARAMETERS

-0, --null
    Input items are terminated by a null character instead of by whitespace. This is crucial for handling filenames or other data that may contain spaces, newlines, backslashes, or quotes, making it safe for use with commands like `find -print0`.

-I replace-str
    Replace occurrences of replace-str in the initial arguments with names read from standard input. Also, blanks and newlines are not considered as delimiters. This implies -L 1. Useful when the command needs specific argument placement, like `cp {} /destination/`.

-L max-lines
    Use at most max-lines non-blank input lines per command line. Trailing blanks will cause the next input line to be treated as part of the current line.

-n max-args
    Use at most max-args arguments per command line. Fewer arguments are used if the maximum command line length (see -s) is exceeded.

-P max-procs, --max-procs=max-procs
    Run up to max-procs processes at a time. The default is 1. If max-procs is 0, xargs will run as many processes as possible simultaneously.

-p, --interactive
    Prompt the user whether to run each command line and read a line from the terminal. Only run the command if the response starts with 'y' or 'Y'.

-r, --no-run-if-empty
    If there are no arguments from standard input, do not run the command. This is the default for GNU xargs and is often useful to prevent commands from being executed with no arguments when input is empty.

-s max-chars, --max-chars=max-chars
    Use at most max-chars characters per command line, including the command and initial arguments, and the terminating nulls at the ends of the strings.

-t, --verbose
    Print the command line to standard error before executing it. Useful for debugging and understanding what xargs is doing.

DESCRIPTION

The xargs command reads items from standard input, delimited by blanks (which can be escaped or quoted) or newlines, and executes a specified command one or more times with these items as arguments. It is primarily used to overcome the operating system's command line length limit for arguments, allowing commands to process a very large number of files or items that might otherwise cause an 'argument list too long' error.

xargs is highly versatile, enabling the output of one command (e.g., find) to be efficiently piped and used as arguments for another command (e.g., rm, mv, grep). It offers powerful options to control how arguments are grouped, how many commands are run in parallel, and how special characters in filenames (like spaces or newlines) are handled, making it an indispensable tool for shell scripting and automation.

CAVEATS

Using xargs with default settings can be problematic when dealing with filenames or data containing spaces, newlines, or other special characters, as these are typically treated as delimiters. Always prefer the -0 option in conjunction with commands that produce null-terminated output (like `find -print0`) for robust and reliable processing of arbitrary filenames.

While xargs helps circumvent argument list limits, it still has its own internal limit on the total command line length (controlled by -s), which, if exceeded, may lead to errors or unexpected behavior depending on the options used. Command failures are typically not propagated back as errors to xargs by default, so you might need to check the exit status of the executed command within a subshell or use `sh -c 'command args'` to handle errors more gracefully.

COMMON USAGE PATTERNS

One of the most common and robust ways to use xargs is with `find` to process files, especially those with problematic names:
find . -type f -name "*.log" -print0 | xargs -0 rm
This ensures that filenames containing spaces or newlines are handled correctly.

Another common pattern involves executing a command per item, such as copying files:
ls *.txt | xargs -I {} cp {} /backup/dir/
Here, `{}` acts as a placeholder for each input line.

PERFORMANCE WITH PARALLEL EXECUTION

For tasks that can be parallelized, the -P option can significantly speed up execution. For example, converting multiple images using `convert`:
find . -name "*.jpg" -print0 | xargs -0 -P 4 convert -quality 75 {} {}.webp
This command would run up to 4 `convert` processes simultaneously, taking advantage of multi-core processors. Be mindful of system resources when using high values for -P.

HISTORY

The xargs command has been a part of Unix-like operating systems since the early days, primarily introduced to handle the `ARG_MAX` limitation – the maximum length of arguments to a command. As the number of files or items grew, passing them directly to commands could easily exceed this limit, leading to errors. xargs provided an elegant solution by splitting the input into smaller chunks and executing the command multiple times.

Its development has focused on robustness, particularly in handling special characters in filenames, leading to the widely adopted -0 option in the GNU version. This feature, along with parallel execution capabilities (-P), has cemented xargs's role as a powerful and essential utility in the Unix toolkit.

SEE ALSO

find(1), grep(1), rm(1), mv(1), cp(1), bash(1), sh(1), pipes(7)

Copied to clipboard