Linux xargs Command: Syntax, Options, Examples

December 5, 2024

Introduction

Some Linux commands accept input from both standard input (stdin) and command-line arguments, while others are designed to work primarily with command-line arguments.

The xargs command bridges this gap by converting input from stdin into arguments for commands that do not process stdin directly.

In this tutorial, you will learn how to use the Linux xargs command to manipulate the standard input and work with other commands.

Linux xargs Command: Syntax, Options, Examples

Prerequisites

  • A system running Linux.
  • Access to the terminal.

Linux xargs Command Syntax

The xargs command syntax is:

xargs [options] [command]

Options are additional flags that modify the xargs behavior. The [command] parameter is also optional. If you don't specify a command, xargs uses the default command echo to output the arguments it receives.

xargs Command Options

There are several xargs command options. The following table presents the most common ones:

OptionDescription
-0, --nullTreats input items as separated by null characters instead of the usual whitespace or newline delimiters.
-n, --max-argsLimits the number of arguments per command.
-d, --delimiterUses a custom character as the delimiter.
-I, --replaceReplaces a placeholder with arguments from input.
-L, --max-linesLimits the number of lines of input per command.
-P, --max-procsRuns multiple commands in parallel.
-t, --verbosePrints the command before executing.
-r, --no-run-if-emptyDoes not run if the input is empty.
-ESets the end-of-file string.
-LLimits input lines.
-IReplaces placeholders
-a file, --arg-file=fileReads input from a file instead of standard input.
-o, --open-ttyReopens stdin as /dev/tty for interactive applications.
--process-slot-var=nameSets an environment variable unique to each child process. Useful for managing parallelism.
-s max-chars, --max-chars=max-charsLimits the total number of characters per command, including arguments.
--show-limitsDisplays system-imposed limits on the command-line length.
--Stops option parsing. Useful when dealing with commands or arguments starting with -.
--helpDisplays help information.
--versionShows the version number.

xargs Examples 

There are plenty of practical use-case examples for the xargs command. The following text elaborates on them.

Combine xargs with find

The find command often precedes xargs in a pipeline. Use it to provide a list of files for further processing by xargs. The syntax looks like this:

find [location] -name "[search-term]" -type f | xargs [command]

The syntax consists of:

  • find. Searches for files or directories.
  • [location]. Specifies the directory where the search begins.
  • -name "[search-term]". Limits the search to files (or directories) matching the given name pattern.
  • -type f. Restricts the search to files (f for files). Use -type d to search for directories.
  • |. Takes the find output (a list of files) and passes it as input to the next command (xargs).
  • xargs. Reads the input (list of files) and executes the specified [command] with the files as its arguments.
  • [command]. Accepts file paths as input, such as rm, ls, or cat.

For instance, the following example demonstrates using the find command to find all files with the .sh extension. The list of files is then piped to xargs, which uses the ls command to display them:

find /home/sara -name '*.sh' -type f | xargs ls
find /home/sara -name '*.sh' -type f | xargs ls terminal output

However, xargs does not automatically include files that contain blank spaces in their names. To include those files, too, use the -print0 option for find and the -0 option for xargs:

find [location] -name "[search-term]" -type f -print0 | xargs -0 [command]

Combine xargs with grep

Use xargs with the grep command to search for a string in the list of files the find command provides. Use the following syntax:

find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'

For example, search for all the files with the .txt extension and pipe them to xargs, which then executes the grep command on them:

find /home/sara -name '*.txt' | xargs grep example
find /home/sara -name '*.txt' | xargs grep example terminal output

xargs Multiple Commands

To run more than one command with xargs, use the -I option. The syntax is:

[command-providing-input] | xargs -I % sh -c '[command-1] %; [command-2] %'

The syntax consists of:

  • xargs -I %. Tells xargs to replace % with the input it receives from the previous command in the pipeline.
  • sh -c '[command-1] %; [command-2] %'. Runs multiple commands for each input, using % as a placeholder.

For example, display the file4.txt contents first. Then, create a directory for each word in the file with mkdir:

cat file4.txt | xargs -I % sh -c 'echo "%"; mkdir "%"'
terminal output for cat file4.txt | xargs -I % sh -c 'echo "%"; mkdir "%"'

Read Items From File

The xargs command reads the standard input. Use the -a option to read the file contents instead. The syntax is:

xargs -a [filename]

For example, to read the test1.txt contents, run:

xargs -a test1.txt
xargs -a test1.txt terminal output

Find and Archive Images Using tar

When used with the tar command, xargs creates a tar.gz archive and populates it with files provided by the find command. The syntax is:

find [location] -name "[search-term]" -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]

Use the following command to find all .png files in the /home/sara directory and its subdirectories, outputting each file with a null character separating the file names.

Next, the command takes the list of .png files (from find), and passes them to tar to create a .tar.gz archive named photos.tar.gz, adding each file to the archive.

find /home/sara -name "*.png" -type f -print0 | xargs -0 tar -cvzf photos.tar.gz
find /home/sara -name "*.png" -type f -print0 | xargs -0 tar -cvzf photos.tar.gz terminal output

Verify the existence of the new archive with ls:

ls
ls terminal output

The -t option in xargs causes it to print the commands it is about to execute to the standard output. This shows what xargs is doing with the input it's receiving before it executes the command. The syntax is:

[command-providing-input] | xargs -t [command]

For example, xargs executes the mkdir command on the entire string provided by echo:

echo "directory4 directory5 directory6" | xargs -t mkdir
echo "directory4 directory5 directory6" | xargs -t mkdir terminal output

Approve xargs Command Execution

Some xargs operations, like removing files and directories, are irreversible. To control the execution of those commands, use the -p option. The syntax is:

[command-providing-input] | xargs -p [command]

For example, to verify the removal of file.txt, run:

echo "file.txt" | xargs -p rm
echo "file.txt" | xargs -p rm terminal output

When you run the command with the -p option, xargs displays a confirmation line before executing it. Type y to proceed or n to cancel the operation.

Limit Output per Line

Sometimes, it is necessary to control the number of arguments xargs takes at the same time. Perform this action using the -n option followed by the number of arguments you are limiting xargs to:

[command-providing-input] | xargs -n [number] [command]

In the example below, xargs takes the string from the echo command and splits it into groups of three. Then, it executes another echo for each group. In this case, it prints the numbers in sets of three per line.

Run the following command:

echo "1 2 3 4 5 6 7 8 9" | xargs -n 3
echo "1 2 3 4 5 6 7 8 9" | xargs -n 3 terminal output

Specify the Delimiter

The default xargs delimiter is a blank space. To change the default delimiter, use the -d argument followed by a single character or an escape character, such as n (a new line).

The syntax is:

[command-providing-input] | xargs -d [new-delimiter] [command]

In the example below, the xargs command instructs the system to use * as a delimiter and apply mkdir to each of the obtained arguments.

echo -n "directoryA<em>directoryB</em>directoryC" | xargs -d '*' mkdir
echo -n "directoryAdirectoryBdirectoryC" | xargs -d '*' mkdir terminal output

List All Linux User Accounts on the System

Use xargs to organize the output of the commands, such as cut. Consider the following example:

cut -d: -f1 < /etc/passwd | sort | xargs
cut -d: -f1 < /etc/passwd | sort | xargs terminal output

The cut command accesses the /etc/passwd file and uses the : delimiter to cut the beginning of each line in the file. The output is then piped to sort to sort the received strings, and to xargs that displays them.

Note: For alternative ways to list users, read How to List Users in Linux.

Remove Blank Spaces in String

Since xargs ignores blank spaces when looking for arguments, the command is useful for removing unnecessary blank spaces from strings.

echo "[string-with-unnecessary-spaces]" | xargs

For example, remove all spaces in a line Line with spaces, with:

echo "  Line  with  spaces" | xargs
echo " Line with spaces" | xargs terminal output

List Number of Lines/Words/Characters in Each File

Use xargs with the wc command to display a list of files with the line, word, and character count.

For example, instruct the ls command to pipe to xargs only the files containing the word example. xargs then applies wc to that list:

ls *example* | xargs wc
ls *example* | xargs wc terminal output

Copy File to Multiple Directories

Copy files to multiple directories using the xargs command. The syntax is:

echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]

For example, to copy the file file.txt into each directory listed in the echo command output, one directory at a time, run:

echo ./Documents/ ./Public/ ./Desktop/ | xargs -n 1 cp -v file.txt
echo ./Documents/ ./Public/ ./Desktop/ | xargs -n 1 cp -v file.txt terminal output

The echo command provides directory names while xargs uses the cp command to copy the given file into each of the directories.

Conclusion

This tutorial explained how to use the xargs command in combination with other commands to handle and process input efficiently in Linux.

Next, learn how to use awk and sed for advanced text processing and formatting.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use the Linux tee Command
December 3, 2024

By default in Linux, a command received through standard input writes directly to standard output. The tee...
Read more
How to Use Linux Cat Command (With Examples)
June 3, 2024

The cat command, short for concatenate, is used to display the contents of one or more files, without having...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Extract or Unzip tar.gz Files from Linux Command Line
February 12, 2024

This article shows which commands best to use when compressing and decompressing files from the command line.
Read more