What is the xargs command?
The xargs
command constructs and executes commands provided through standard input. It takes input and converts it into command arguments for another command. This functionality is particularly useful in file management and can be combined with commands like rm
, cp
, mkdir
, and others.
Using the xargs command
When used alone, the xargs command prompts the user to enter a text string and then passes it to the echo command.
This example demonstrates the input provided and the output of the echo command.
Combining xargs with find
The find
command is often used in conjunction with xargs
in a pipeline. It provides a file list to xargs
for further processing. The syntax is as follows:
find [location] -name "[search-term]" -type f | xargs [command]
The above example demonstrates the use of the find
command to locate all files with a .sh extension. The file list is then passed through a pipeline to xargs
, which uses the rm
command to delete them.
However, xargs
does not automatically handle files with spaces in their names. To include these files as well, use the -print0
option with find and the -0
option with xargs:
find [location] -name "[search-term]" -type f -print0 | xargs -0 [command]
The rm
command is now used to delete all files with the .sh extension.
Combining xargs with grep
xargs
can be used with the grep
command to search for a string in the file list provided by that command.
find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'
The above example searches for all files with the .txt extension and pipes them to xargs
, which then executes the grep
command on them.
Multiple xargs commands
To run multiple commands with xargs
, use the -I option. The syntax is as follows:
[command-providing-input] | xargs -I % sh -c '[command-1] %; [command-2] %'
In the example, the contents of file4.txt are displayed first. Then, mkdir
is used to create a folder for each word in the file.
Reading content from a file
As mentioned earlier, xargs
reads from standard input. Use the -a option to read the content of a file.
xargs -a [filename]
Combining find and tar
When used together with the tar
command, xargs
creates a tar.gz archive and populates it with the files provided by the find command.
find [location] -name "[search-term]" -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]
Printing commands
To view the commands being executed in the standard output by xargs
, use the -t option.
[command-providing-input] | xargs -t [command]
In the above example, note that the entire string provided was used by xargs
to execute the mkdir
command.
Interactive execution of xargs commands
Some xargs
operations, such as deleting files and folders, are irreversible. To control the execution of these commands, use the -p option.
[command-providing-input] | xargs -p [command]
When you use the -p option to execute a command, xargs
displays a confirmation prompt before execution. Type 'y' to proceed or 'n' to cancel the operation.
Limiting output per line
Sometimes you need to control the number of arguments xargs
accepts at a time. To do this, use the -n option followed by the number of arguments you want to limit xargs
to.
[command-providing-input] | xargs -n [number] [command]
In the example below, xargs
takes the string "echo" from the command and splits it into three parts. Then, it executes another "echo" for each part:
Specifying a delimiter
By default, the delimiter used by xargs
is a space. To change the default delimiter, use the -d option followed by a single character or an escape character (such as 'n' for a new line) in the command.
[command-providing-input] | xargs -d [new-delimiter] | xargs [command]
In the example below, the xargs
command instructs the system to use an asterisk (*) as a delimiter and applies it to mkdir for each parameter obtained.
Removing spaces from strings
Since xargs
ignores spaces when parsing arguments, this command is useful for removing unnecessary spaces from a string.
echo "[string-with-unnecessary-spaces]" | xargs
Listing the line count/word count/character count in each file
xargs
can be used with the wc
command to display a file list along with the line count, word count, and character count.
ls | xargs wc
The example below instructs the ls command to only pass files containing the word "example" through the pipeline to xargs
. xargs
then applies wc
to this list:
Copying files to multiple directories
To copy files to multiple directories using xargs
, the syntax is straightforward:
echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]
The echo command provides the directory names, and xargs
uses the cp
command to copy the given file to each directory.
Reference: 如何使用 Linux xargs 命令