Introduction to the

Xargs can convert input content (usually piped through the command line) into parameters for subsequent commands. Common uses include:

  1. Command composition: In particular, some commands do not support pipeline input, such asls.
  2. Avoid too long arguments: XARgs can pass-nxTo group parameters so that they are not too long.

Use the following syntax

Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.Copy the code

Introductory example

First, create the test file

touch a.js b.js c.jsCopy the code

Next, run the following command:

ls *.js | xargs ls -alCopy the code

The output is as follows:

-rw-r--r--  1 a  wheel  0 12 18 16:18 a.js
-rw-r--r--  1 a  wheel  0 12 18 16:18 b.js
-rw-r--r--  1 a  wheel  0 12 18 16:18 c.jsCopy the code

Command explanation:

  1. First of all,ls *.jsThe output of the fora.js b.js c.js.
  2. Through the pipe, willa.js b.js c.jsAs axargsInput parameter of.
  3. xargsAfter receiving the input parameter, the command parses the parameter, using space/newline as separator, and splits the parameter into multiple parametersa.js,b.js,c.js.
  4. xargsThe split parameters are passed to the subsequent command as parameters to the subsequent command, that is, to form such a commandls -al a.js b.js c.js.

You can add the -t parameter to print the command before executing the following command.

ls *.js | xargs -t ls -alCopy the code

The output is ls -al a.js b.js c.js. This is the actual command to run.

ls -al a.js b.js c.js
-rw-r--r--  1 a  wheel  0 12 18 16:18 a.js
-rw-r--r--  1 a  wheel  0 12 18 16:18 b.js
-rw-r--r--  1 a  wheel  0 12 18 16:18 c.jsCopy the code

Example: Parameter substitution

Sometimes, we need to use the original argument, either with the -i or -i argument. Parameters are described as follows:

  -I R                         same as --replace=R (R must be specified)
  -i,--replace=[R]             Replace R in initial arguments with names
                               read from standard input. If R is
                               unspecified, assume {}Copy the code

For example, add the.backup suffix to all.js files. -i ‘{}’ replaces {} on the command line with the parsed parameters.

ls *.js | xargs -t -I '{}' mv {} {}.backupCopy the code

The expanded command is as follows:

mv a.js a.js.backup
mv b.js b.js.backup
mv c.js c.js.backupCopy the code

Example: Parameter grouping

The command line has a limit on the maximum length of arguments, and Xargs solves this problem by grouping arguments by -nx.

First, create four files to experiment with.

touch a.js b.js c.js d.jsCopy the code

Then run the following command:

ls *.js | xargs -t -n2 ls -alCopy the code

The output is shown as -n2, passing the parameters as a group of two to the command that follows.

ls -al a.js b.js 
-rw-r--r-- 1 root root 0 Dec 18 16:52 a.js
-rw-r--r-- 1 root root 0 Dec 18 16:52 b.js
ls -al c.js d.js 
-rw-r--r-- 1 root root 0 Dec 18 16:52 c.js
-rw-r--r-- 1 root root 0 Dec 18 16:52 d.jsCopy the code

Example: special file name

Sometimes the file name may contain special characters, such as Spaces in the file name below.

touch 'hello 01.css' 'hello 02.css'Copy the code

Running the previous command gives an error because Xargs uses a space/newline separator, and unexpected behavior occurs.

. # command find -name '*. CSS' | xargs -t ls - al # output ls - al. / hello. 01 CSS. / hello 02. CSS # unfolds the command ls: cannot access ./hello: No such file or directory ls: cannot access 01.css: No such file or directory ls: cannot access ./hello: No such file or directory ls: cannot access 02.css: No such file or directoryCopy the code

Here’s how XARgs solves this problem.

  1. -print0: tellfindCommand, following the output file nameNULLCharacters instead of newlines;
  2. 0: tellxargsIn order toNULLAs a parameter separator;
find . -name '*.css' -print0 | xargs -0 -t ls -alCopy the code

Example: Log backup

Back up logs generated seven days ago to a specific directory

find . -mtime +7 | xargs -I '{}' mv {} /tmp/otc-svr-logs/Copy the code

A link to the

craftsmanbai.gitbooks…

wiki.jikexueyuan.com/p…