A primer example of the Linux base xargs command

  • 2020-06-23 02:38:04
  • OfStack

Introduction to the

The xargs command has two main points. First, you must list the destination file. Second, you must specify the commands or scripts to execute on each file.

The xargs command is used to process files distributed in different directories:

Count the lines of all files Prints line 1 of the specified file Execute 1 custom script per file

xargs can turn input (usually piped through the command line) into arguments to subsequent commands, typically for:

Command combinations: in particular, 1 some commands do not support piped input, such as ls. Avoid too long parameters: xargs can be grouped by -ES14en to avoid too long parameters.

Use the following syntax


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

Introductory example

First, create the test file


touch a.js b.js c.js

Next, run the following command:


ls *.js | xargs ls -al

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

Explanation of command:

First, the output of ls *.js is a.js b.js c.js. Through the pipe, a.js ES42en.js ES44en.js is the input parameter of xargs. After receiving the input parameters, the xargs command parses the parameters and splits them into multiple parameters with space/line break as delimiter, which becomes a.js, ES50en.js, c.js. xargs passes the split parameter to the subsequent command as the parameter of the subsequent command, that is, forms the command ls-ES56en a.js c.js.

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


ls *.js | xargs -t ls -al

The output is as follows. You can see that there is an extra line of ls-ES69en a.js b.js c.js. This is the actual command that is running.


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

Example: Parameter substitution

Sometimes, we need to use the original parameter, which can be implemented with the parameter -ES82en or -ES83en. The 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 {}

As an example, add the.backup suffix to all files ending in.js. -I '{}' means to replace the following command line {} with the previously resolved argument.


ls *.js | xargs -t -I '{}' mv {} {}.backup

The expanded command is as follows:


mv a.js a.js.backup
mv b.js b.js.backup
mv c.js c.js.backup

Example: Parameter grouping

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

First, create four files for the experiment.


touch a.js b.js c.js d.js

Then run the following command:


touch a.js b.js c.js
0

The output is as follows, -n2 represents, and the arguments are grouped in groups of two and passed to the following command.


touch a.js b.js c.js
1

Example: Special file names

Sometimes filenames may have special characters, such as Spaces in the following filename.


touch a.js b.js c.js
2

Running the previous command will report an error because xargs is delimited by a space/line break, resulting in unexpected behavior.


#  The command 
find . -name '*.css' | xargs -t ls -al
# The output 
ls -al ./hello 01.css ./hello 02.css #  Expand 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 directory

xargs solves this problem in this way.

-print0: Tells the find command to follow the NULL character after the output file name, not the newline; -0: Tells xargs to take NULL as the parameter separator;

touch a.js b.js c.js
4

Example: Log backup

Back up the logs from 7 days ago to a specific directory


touch a.js b.js c.js
5

conclusion

A link to the

https://craftsmanbai.gitbooks...

http://wiki.jikexueyuan.com/p...


Related articles: