The Bridge Between Linux Pipes and Command Line Arguments: xargs

<span>xargs</span> is a very powerful Unix/Linux command used to build and execute command lines from standard input. It is often used in conjunction with <span>pipes (|)</span> to convert the output of the previous command into parameters for the next <span>command.</span>

<span>xargs</span> command format is as follows:

xargs [-options] [command]

Generally used in conjunction with <span>pipes (|)</span>:

command1 | xargs [-options] [command2]

<span>For example:</span>

# Create test directory
echo "test" | xargs mkdir
  • <span>echo "test"</span>: Outputs the string “test” to standard output
  • <span>|</span>: Pipe, passing the output of the previous command as input to the next command
  • <span>xargs mkdir</span> reads data from standard input and passes it as parameters to the mkdir command
    • Constructs the command <span>mkdir test</span> and executes it

By default, the command following xargs is <span>echo</span>:

$ echo 123 | xargs
# Equivalent to
$ echo 123 | xargs echo

Core Options

If you are still confused as a beginner, don’t worry, keep reading.

-p: Interactive Mode

Confirms before executing each command.

# -p: Will output the command constructed by xargs first and ask for confirmation before execution.
$ echo test | xargs -p mkdir
mkdir test?...y

# test directory created successfully.

<span>-t</span>: Verbose Mode

Prints the command on standard error output before executing it. Unlike <span>-p</span>, it does not ask interactively.

$ echo test2 | xargs -t mkdir
mkdir test2 # This is the printed command.
# test2 directory created successfully.

<span>-d</span>: Custom Delimiter

By default, xargs uses newline and space as delimiters to break standard input into command line parameters.

$ echo "one two three" | xargs -t echo

Output:

/bin/echo one two three  # This line outputs the constructed command
one two three            # This is the result of the command

Now using the <span>-d</span> option can change the delimiter.

$ echo "a,b,c" | xargs -t -d "," echo

Output:

/bin/echo a b c  # This line outputs the constructed command
a b c            # This is the result of the command

<span>Notably</span>, the xargs on macOS does not have this option

# Alternative on macOS
$ echo "a,b,c" | tr ',' '\n' | xargs

<span>-n</span>: Maximum Number of Arguments

The above example concatenates all the separated <span>items</span> as parameters for the command at once. You can specify the maximum number of arguments used each time the command is executed with<span>-n</span>.

echo "1 2 3 4 5" | xargs -n 2

Output:

1 2
3 4
5

<span>Debugging</span>, add the <span>-t</span> option, as mentioned above, the default command here is <span>echo</span>.

echo "1 2 3 4 5" | xargs -n 2 -t

Output:

/bin/echo 1 2  # This line is the constructed command
1 2            # Result
/bin/echo 3 4  # This line is the constructed command
3 4            # Result
/bin/echo 5    # This line is the constructed command
5              # Result

-I: Replace String

Specifies a template string for command construction<span>replacement</span>

First, look at the case without specifying <span>-I</span>:

$ echo "dir_name" | xargs mkdir
  • The constructed command is: <span>mkdir dir_name</span>
  • By default, xargs appends parameters directly at the end. However, sometimes you need to<span>specify the position yourself.</span>

Specifying <span>-I</span>:

$ echo "test.txt test2.txt" | xargs -n 1 -I {} mv {} backup/
  • <span>Function</span>: Moves test.txt and test2.txt files to the backup target.
  • <span>-n</span>: Specifies the number as 1, only passing one item when constructing the command.
  • <span>-I</span>: Specifies the template string for replacing the item as: <span>{}</span>, mv uses <span>{}</span> as a placeholder for the first parameter.
  • Generates the following commands:
    • <span>mv test.txt backup/</span>
    • <span>mv test2.txt backup/</span>

Option Reference

Option Full Name Description Common Scenarios
-I {} –replace Defines the replacement string When the command needs parameters in the middle
-n NUM –max-args Maximum number of arguments used per command Batch processing
-t –verbose Prints the command to be executed Debugging
-p –interactive Confirms before execution Before dangerous operations
0 –null Separates input with null characters Handling filenames with spaces
-a FILE –arg-file Reads parameters from a file When the parameter list already exists in a file
-r –no-run-if-empty Does not run the command if input is empty Avoids empty execution
-s NUM –max-chars Maximum command line length Avoids “Argument list too long”
-P NUM –max-procs Maximum number of parallel processes Accelerates a large number of tasks
-L NUM –max-lines Maximum number of lines read at a time Processes input by line
-d DELIM –delimiter Specifies the input delimiter Handles non-standard separated data
-E STR –eof Specifies the end-of-input marker Terminates input early

In Summary

<span>xargs</span>’s core value lies in its ability to cleverly convert data into command line parameters, thereby indirectly and efficiently achieving a “traversal” operation on a set of targets.

We can understand this process as: <span>xargs</span> constructs and executes an implicit <span>for</span> loop.

xargs itself does not have loop syntax like for or while; its workflow is as follows:

  • <span>Receive Input</span>: Reads data from standard input (stdin), usually passed through <span>pipes (|)</span>.

  • <span>Process Parameters</span>: Splits the input data into parameters based on options (e.g., -n 1 means one parameter per line, -I {} specifies the replacement string).

  • <span>Construct and Execute Command</span>: Inserts these parameters one by one or in batches into the command template specified after it and executes this newly constructed command.

Leave a Comment