Linux xargs Command: Pass Standard Input as Command Arguments

xargs reads items from standard input and passes them as arguments to other commands. It is used when the output of a pipe should be treated as arguments like filenames rather than input data to a command.

What is the xargs command?

The default delimiters are spaces and newlines, and quotes and backslashes are also interpreted. Therefore, when processing arbitrary filenames, pair find -print0 with xargs -0 to safely separate them with a NUL character.

Basic syntax

Generate command | xargs [options] execute command [fixed arguments...]

The installed implementation and options may vary depending on the distribution. Check the description on your current system with man xargs.

Examples

Passing values as arguments

Groups input lines one or more at a time and passes them to a command.

printf '%s\n' one two three | xargs printf '%s\n'

Safely passing filenames

Preserves filenames with spaces or newlines by using NUL separation.

find reports -type f -name '*.txt' -print0 | xargs -0 -r wc -l

Execute one at a time

If separate execution for each item is required, use -n 1.

printf '%s\n' alpha beta | xargs -n 1 printf 'item: %s\n'

The process of changing standard input to arguments

xargs passes input items as arguments to the command specified after it. -n 1 runs the command once for each item.

printf 'one\ntwo\n' | xargs -n 1 printf 'item=%s\n'

Example output:

item=one
item=two

By default, xargs interprets spaces, quotes, and backslashes as delimiters, so it is risky to use with a list of filenames containing spaces or newlines. When passing filenames, use the NUL-separated output of the generating command together with xargs -0. Before connecting it to commands that make changes, such as deletion, it is safer to first check the arguments to be passed with printf.

Do not treat newline-separated text as a safe filename list when names can contain spaces or shell metacharacters. Match the delimiters at both ends of a pipeline, for example with find ... -print0 | xargs -0 .... GNU xargs -r avoids running the command when there is no input.

Main Options and Format

Options/Format Description
-0 Separate items with the NUL character.
-r In the GNU implementation, it does not execute if the input is empty.
-n N Pass up to N arguments per command execution.
-P N Execute up to N commands in parallel.
-I placeholder Substitute input items at the specified position in the command.

Precautions when using

If you pass file names based on line breaks, like find ... -print | xargs rm, names containing spaces or newlines will be incorrectly split. For deletion tasks, use NUL separation or find -exec and check the targets first. Parallel execution with -P changes output order and resource contention.

Frequently Asked Questions

How is using only a pipe different from xargs?

command1 | command2 sends it to the standard input of command2. xargs constructs the values read as arguments to command2.

Official Documentation

You can check the exact behavior of options and implementation differences on the xargs manual page.

More in This Category
Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Learn how to inspect and Configure Network Interfaces and Routes with the Linux ip command, including practical examples, key options, and important precautions.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux shutdown Command: Schedule a Shutdown or Restart

Linux shutdown Command: Schedule a Shutdown or Restart

Learn how to schedule a Shutdown or Restart with the Linux shutdown command, including practical examples, key options, and important precautions.

Linux xargs Command: Pass Standard Input as Command Arguments

Linux xargs Command: Pass Standard Input as Command Arguments

Learn how to pass Standard Input as Command Arguments with the Linux xargs command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname command, including practical examples, key options, and important precautions.

Linux ssh-copy-id Command: Install a Public Key on a Remote Account

Linux ssh-copy-id Command: Install a Public Key on a Remote Account

Learn how to install a Public Key on a Remote Account with the Linux ssh-copy-id command, including practical examples, key options, and important precautions.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.