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.
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:
Option | Description |
---|---|
-0 , --null | Treats input items as separated by null characters instead of the usual whitespace or newline delimiters. |
-n , --max-args | Limits the number of arguments per command. |
-d , --delimiter | Uses a custom character as the delimiter. |
-I , --replace | Replaces a placeholder with arguments from input. |
-L , --max-lines | Limits the number of lines of input per command. |
-P , --max-procs | Runs multiple commands in parallel. |
-t , --verbose | Prints the command before executing. |
-r , --no-run-if-empty | Does not run if the input is empty. |
-E | Sets the end-of-file string. |
-L | Limits input lines. |
-I | Replaces placeholders |
-a file , --arg-file=file | Reads input from a file instead of standard input. |
-o , --open-tty | Reopens stdin as /dev/tty for interactive applications. |
--process-slot-var=name | Sets an environment variable unique to each child process. Useful for managing parallelism. |
-s max-chars , --max-chars=max-chars | Limits the total number of characters per command, including arguments. |
--show-limits | Displays system-imposed limits on the command-line length. |
-- | Stops option parsing. Useful when dealing with commands or arguments starting with - . |
--help | Displays help information. |
--version | Shows 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 thefind
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
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
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 %
. Tellsxargs
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 "%"'
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
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
Verify the existence of the new archive with ls
:
ls
Print Command
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
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
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
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
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
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
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
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
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.