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

SYNOPSIS

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

PARAMETERS

-0, --null
    Input items are terminated by a null character instead of whitespace. Useful when dealing with filenames containing spaces or special characters.

-a, --arg-file=FILE
    Read items from FILE instead of standard input.

-d, --delimiter=DELIM
    Use DELIM as a delimiter to separate input items.

-E EOF-STR
    Stop reading standard input once EOF-STR is encountered.

-I REPLACE-STR
    Replace each occurrence of REPLACE-STR in the initial-arguments with names read from standard input. Implies -x and -L 1.

-L MAX-LINES
    Process at most MAX-LINES nonblank input lines per command line.

-n, --max-args=MAX-ARGS
    Use at most MAX-ARGS arguments per command line.

-P, --max-procs=MAX-PROCS
    Run at most MAX-PROCS processes at a time; the default is 1. If MAX-PROCS is 0, run as many processes as possible.

-s, --max-chars=MAX-CHARS
    Use at most MAX-CHARS characters per command line, including the command and initial arguments.

-t, --verbose
    Print the command line on standard error before executing it.

-x, --exit
    Exit if any command exits with a non-zero status.

--help
    Display help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The xargs command is a powerful utility in Linux and Unix-like operating systems used to build and execute command lines from standard input. It reads items from standard input, which are typically separated by whitespace (spaces, tabs, newlines), and appends them as arguments to a specified command. This allows you to process large lists of files or other data that would exceed the command-line length limit if passed directly.
xargs is particularly useful in conjunction with commands like find, grep, and ls, which can generate lists of items that need to be further processed. By piping the output of these commands to xargs, you can efficiently perform operations on multiple files or data entries at once.
The command offers various options to customize its behavior, such as limiting the number of arguments passed to each command invocation, specifying a delimiter other than whitespace, or prompting the user for confirmation before executing each command.

CAVEATS

Be mindful of command-line length limits. The `-s` option helps mitigate issues, but it's crucial to understand the limitations of your system.
Without `-0` or a suitable delimiter, filenames containing spaces or other special characters may be mishandled.
When using `-I`, ensure that the REPLACE-STR is unique and doesn't appear unintentionally in the initial-arguments.

SECURITY CONSIDERATIONS

When dealing with untrusted input, avoid using xargs without proper sanitization. Malicious input could potentially lead to command injection vulnerabilities. The `-0` option and careful quoting can help mitigate these risks.
The `-I` option also introduces risk if the replacement string is derived from untrusted sources.

EXAMPLES

Find and delete files:
find . -name "*.tmp" -print0 | xargs -0 rm -f

Grep for a string in multiple files:
ls *.txt | xargs grep "pattern"

Rename files using `-I`:
find . -name "*.old" -print0 | xargs -0 -I {} mv {} {}.new

SEE ALSO

find(1), grep(1), ls(1), cut(1), sed(1), awk(1)

Copied to clipboard