xargs command tutorial

xargsIt 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/passwdconversion) as a standard input standard output, a command is provided to the right ( grep root) as a parameter.

Because the grepcommand 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, echothe command transfer pipe not accept parameters.


$ echo "hello world" | echo

The above code does not have output. Since the right side of the pipeline echodoes not accept standard input conduit coming as a parameter.

Second, the role xargs command

xargsThe 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 echocommand.

xargsCommand format is as follows.


$ xargs [-options] [command] 

Real command execution, immediately xargsbehind, accepts xargsparameters coming.

xargsThe effect is that most commands (such as rm, , mkdir) lswhen used together with the pipe, are required xargsto 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 xargsit will error, suggesting mkdira lack of operating parameters.

Three, xargs used alone

xargsBehind the command defaults echo.


$ xargs
# 等同于
$ xargs echo

Most of the time, xargsthe command is used together with the pipeline. However, it can also be used alone.

Enter xargsPress 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 echocommand 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 -namelater, 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, xargsthe 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, mkdirwill be three new subdirectory, as xargswill one two threebe broken down into three command line parameters, performs mkdir one two three.

-dParameters can change the separator.


$ echo -e "a\tb\tc" | xargs -d "\t" echo a b c 

The above command specifies tab \tas the separator, it a\tb\tcis transformed into three command line arguments. echoCommand -eparameter indicates explain the escape character.

Five, -p parameter, -t parameter

Use xargsafter the command, due to the conversion parameters of the process, and sometimes need to check what the command is executed in the end.

-pPrint 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 yafter (upper or lower case), it will really perform.

-tParameter 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 xargsdefault will be a space as a separator, so not suitable for processing the file name, because the file name may contain spaces.

findThere is a special command parameter -print0, specify the output file list to nullseparate. Then, xargsthe command -0parameter represented nullas delimiters.


$ find /path -type f -print0 | xargs -0 rm 

The above command to delete /pathall 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 xargsis particularly suitable for findcommand. Some commands (such as rm) Once the parameters will complain too much "argument list too long", can not be executed in favor xargsdo 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 -Lparameter 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 *.mdtwo rows as a command line parameter passed to findcommand causes an error.

Use -Lparameter 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 echocommand, so the echocommand is executed three times, the output of the three lines.

Eight, -n parameter

-LAlthough 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.

-nParameter 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 echothe command. So, 10 Arabic numerals run five echocommand, the output of the five elements.

Nine, -I parameter

If xargsyou want to command-line arguments passed to multiple commands, you can use -Iparameters.

-IAlternatively 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.txtis a three lines of text files. We hope to every command line parameters, execute two commands ( echoand mkdir) use -I filerepresents filean alternative to the string of command line arguments. When executing commands, specific parameters will replace out echo file; mkdir filetwo inside file.

Ten, - max-procs parameters

xargsThe 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-procsMeanwhile parameter specifies how many processes are executed in parallel command. --max-procs 2Which means both maximum of two processes, --max-procs 0not 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.

Guess you like

Origin www.cnblogs.com/xuefy/p/11328129.html