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 alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux machine-id: How to Check and Reset It for Cloned Systems

Linux machine-id: How to Check and Reset It for Cloned Systems

Learn what the Linux machine-id identifies, how to check it, and how to prepare cloned system images for unique IDs. Understand the difference between an empty file and first-boot initialization.

Linux touch Command: Create Files and Change Timestamps

Linux touch Command: Create Files and Change Timestamps

Learn how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux apt-get Command: Run APT Package Operations in Scripts

Linux apt-get Command: Run APT Package Operations in Scripts

Learn how to run APT Package Operations in Scripts with the Linux apt-get command, including practical examples, key options, and important precautions.

Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Learn how Bash resolves commands and uses exit status with ;, &&, and ||, then distinguish aliases, shell variables, and exported environment variables.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux zypper Command: Manage Packages on openSUSE and SUSE

Linux zypper Command: Manage Packages on openSUSE and SUSE

Learn how to manage Packages on openSUSE and SUSE with the Linux zypper command, including practical examples, key options, and important precautions.