1. Basic usage
xargs
The function of the command is to convert the standard input into command line parameters.
Reason: Most commands do not accept standard input as parameters, and can only enter parameters directly on the command line, which makes it impossible to pass parameters with pipeline commands
For example, echo does not accept standard output as a parameter, and xargs can be used for conversion:
$ echo "hello world" | xargs echo
hello world
2. Parameters
-d
specify delimiter
By default, xargs
newlines and spaces are used as delimiters to break up standard input into individual command-line arguments.
$ echo "one two three" | xargs mkdir
In the above code, mkdir
three subdirectories will be created and executed mkdir one two three
.
-d
Parameters can change the delimiter
$ echo -e "a\tb\tc" | xargs -d "\t" echo
a b c
The above command specifies tab \t
as the delimiter, so a\tb\tc
that translates to three command line arguments. echo
The command's -e
argument indicates the interpretation of escape characters.
-p -t
print the command to be executed
-p
The parameter prints out the command to be executed and asks the user if they want to execute it.
$ echo 'one two three' | xargs -p touch
touch one two three ?...
-t
The parameter is to print out the final command to be executed, and then execute it directly without user confirmation.
$ echo 'one two three' | xargs -t rm
rm one two three
-I
pass parameter alias
If xargs
you want to pass command-line arguments to multiple commands, you can use -I
parameters. [It seems that the parameters will be divided by space or carriage return, and then the command will be executed repeatedly, instead of being treated as multiple parameters of the command]
-I
Specify a replacement string for each command-line argument.
$ cat foo.txt
one
two
three
$ cat foo.txt | xargs -I file sh -c 'echo file; mkdir file'
one
two
three
$ ls
one two three
In the above code, foo.txt
it is a three-line text file. echo
We want to execute two commands ( and mkdir
) for each command-line argument , using the substitution string -I file
indicating that it is a command-line argument. file
When executing the command, the specific parameters will replace echo file; mkdir file
the two inside file
.
-l -L
Specify how many lines to use as a command line argument
$ echo -e "a\nb\nc" | xargs -L 1 echo
a
b
c
-n
Specify multiple items on a line as a command-line argument
$ echo {
0..9} | xargs -n 2 echo
0 1
2 3
4 5
6 7
8 9
--max-procs
multi-threaded execution
xargs
By default, only one process is used to execute commands. If the command needs to be executed multiple times, it must wait for the previous execution to finish before executing the next one.
--max-procs
The parameter specifies how many processes to execute the command in parallel at the same time. --max-procs 2
It means that at most two processes can be used at the same time, --max-procs 0
and it means that the number of processes is not limited.
$ docker ps -q | xargs -n 1 --max-procs 0 docker kill
The above command means to close as many Docker containers as possible at the same time, so that the running speed will be much faster