xargs
It is a very useful command Unix systems, but often overlooked, many people do not understand its use.
This article describes how to use this command.
A standard command input pipe
Unix command has parameters, some commands can accept the "standard input" (stdin) as a parameter.
$ cat /etc/passwd | grep root
The above code uses the Pipe command ( |
). Pipe command action, the command is to the left ( cat /etc/passwd
conversion) as a standard input standard output, a command is provided to the right ( grep root
) as a parameter.
Because the grep
command accepts standard input as a parameter, so that the above code is equivalent to the following code.
$ grep root /etc/passwd
However, most do not accept standard input command as an argument, only the input parameters directly on the command line, which leads to not pass parameters using Pipe command. For example, echo
the command transfer pipe not accept parameters.
$ echo "hello world" | echo
The above code does not have output. Since the right side of the pipeline echo
does not accept standard input conduit coming as a parameter.
Second, the role xargs command
xargs
The role of command, is input into the standard command-line parameters.
$ echo "hello world" | xargs echo hello world
The above code standard left input pipe, into command-line arguments hello world
, passed to the second echo
command.
xargs
Command format is as follows.
$ xargs [-options] [command]
Real command execution, immediately xargs
behind, accepts xargs
parameters coming.
xargs
The effect is that most commands (such as rm
, , mkdir
) ls
when used together with the pipe, are required xargs
to enter into the standard command line parameters.
$ echo "one two three" | xargs mkdir
The above code is equivalent to mkdir one two three
. If not xargs
it will error, suggesting mkdir
a lack of operating parameters.
Three, xargs used alone
xargs
Behind the command defaults echo
.
$ xargs # 等同于 $ xargs echo
Most of the time, xargs
the command is used together with the pipeline. However, it can also be used alone.
Enter xargs
Press Enter after the command line will wait for user input as standard input. You can enter any content, then press Ctrl d
, indicating the end of input, then the echo
command will print out the previous input.
$ xargs hello (Ctrl + d) hello
Look at an example.
$ xargs find -name "*.txt" ./foo.txt ./hello.txt
The above examples of input xargs find -name
later, the command line will wait for user input files to be searched. User input "*.txt"
indicating a search for all TXT files in the current directory, then press Ctrl d
, indicating the end of input. Then quite execution find -name *.txt
.
Four, -d parameter delimiter
By default, xargs
the line breaks and spaces as a delimiter, the standard input is decomposed into a command-line argument.
$ echo "one two three" | xargs mkdir
In the above code, mkdir
will be three new subdirectory, as xargs
will one two three
be broken down into three command line parameters, performs mkdir one two three
.
-d
Parameters can change the separator.
$ echo -e "a\tb\tc" | xargs -d "\t" echo a b c
The above command specifies tab \t
as the separator, it a\tb\tc
is transformed into three command line arguments. echo
Command -e
parameter indicates explain the escape character.
Five, -p parameter, -t parameter
Use xargs
after the command, due to the conversion parameters of the process, and sometimes need to check what the command is executed in the end.
-p
Print out the parameters of the command to be executed, asking if the user wants to perform.
$ echo 'one two three' | xargs -p touch touch one two three ?...
After executing the above command will print out the final command to be executed, allowing users to confirm. User input y
after (upper or lower case), it will really perform.
-t
Parameter is the final print out the command to be executed, and then executed directly, without user confirmation.
$ echo 'one two three' | xargs -t rm rm one two three
Six -0 parameters and find command
Since the xargs
default will be a space as a separator, so not suitable for processing the file name, because the file name may contain spaces.
find
There is a special command parameter -print0
, specify the output file list to null
separate. Then, xargs
the command -0
parameter represented null
as delimiters.
$ find /path -type f -print0 | xargs -0 rm
The above command to delete /path
all the files in the path. Since the separator is null
, so deal with file names that contain spaces, not an error.
There's a reason that xargs
is particularly suitable for find
command. Some commands (such as rm
) Once the parameters will complain too much "argument list too long", can not be executed in favor xargs
do not have this problem, because it commands executed once for each parameter.
$ find . -name "*.txt" | xargs grep "abc"
After the above command to find all TXT files, a search string is included for each file abc
.
Seven, -L parameter
If the standard input contains multiple rows -L
parameter specifies how many rows as a command line argument.
$ xargs find -name "*.txt" "*.md" find: paths must precede expression: `*.md'
While the above command "*.txt"
and *.md
two rows as a command line parameter passed to find
command causes an error.
Use -L
parameter specifying each line as a command line argument, the error will not.
$ xargs -L 1 find -name "*.txt" ./foo.txt ./hello.txt "*.md" ./README.md
The above command specifies each line ( -L 1
) as a command line parameter, respectively, run a command ( find -name
).
Here is another example.
$ echo -e "a\nb\nc" | xargs -L 1 echo a b c
The above code specifies that each line of run time echo
command, so the echo
command is executed three times, the output of the three lines.
Eight, -n parameter
-L
Although the parameters solves the problem of multi-line, but sometimes the user will enter a number in the same row.
$ xargs find -name "*.txt" "*.md" find: paths must precede expression: `*.md'
The above two commands on the same line as a command line parameter, resulting in an error.
-n
Parameter specifies how many entries as command line arguments.
$ xargs -n 1 find -name
The above command specifies each item ( -n 1
) standard input as command line arguments, perform a separate command ( find -name
).
Here is another example.
$ echo {0..9} | xargs -n 2 echo 0 1 2 3 4 5 6 7 8 9
The above command specifies, once every two parameters to run echo
the command. So, 10 Arabic numerals run five echo
command, the output of the five elements.
Nine, -I parameter
If xargs
you want to command-line arguments passed to multiple commands, you can use -I
parameters.
-I
Alternatively a command string specified for each line parameters.
$ 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
The above code foo.txt
is a three lines of text files. We hope to every command line parameters, execute two commands ( echo
and mkdir
) use -I file
represents file
an alternative to the string of command line arguments. When executing commands, specific parameters will replace out echo file; mkdir file
two inside file
.
Ten, - max-procs parameters
xargs
The default command is executed only by a process. If the command to be executed many times, you must wait once executed, to perform next.
--max-procs
Meanwhile parameter specifies how many processes are executed in parallel command. --max-procs 2
Which means both maximum of two processes, --max-procs 0
not to limit the number of processes.
$ docker ps -q | xargs -n 1 --max-procs 0 docker kill
The above command, said while closing as many Docker containers, so the speed will be much faster.