Brief introduction
Xargs can convert input content (typically passed through the command line pipeline) into parameters for subsequent commands, typically using:
- Command combination: In particular, some commands do not support pipeline input, such as
ls .
- Avoid parameters that are too long: Xargs can be used
-nx to group parameters 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.
Getting Started examples
First, create a test file
touch a.js b.js c.js
Next, run the following command:
ls|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
Command explanation:
- First,
ls *.js the output is a.js b.js c.js .
- The input parameters that will be
a.js b.js c.js taken through xargs the pipeline.
xargsAfter the command receives the input parameter, parses the argument, breaks the space/line break as a delimiter, splits into several parameters, here becomes a.js , b.js , c.js .
xargsThe split arguments are passed to subsequent commands as arguments for subsequent commands, that is, to make up such a command ls -al a.js b.js c.js .
You can add a -t parameter to print the command before executing the command that follows it.
ls|xargs -t ls -al
The output is as follows, you can see a line ls -al a.js b.js c.js of content, this is the actual running command.
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 parameters, which can be passed through parameters -i or -I implementations. The parameters are described below
-I R same as --replace=R (R must be specified) -i,--replace=[R] Replace R in initial arguments with names readfromstandardinputIfRis unspecified{}
As an example, add the suffix to all the .js end files .backup . -I ‘{}‘indicates that the following command line is replaced with the {} previously parsed argument.
ls|xargs'{}'{}{}.backup
The following commands are expanded:
mv a.js a.js.backupmv b.js b.js.backupmv c.js c.js.backup
Example: Parameter grouping
The command line has a limit on the maximum length of the parameter, and Xargs -nx solves the problem by grouping the parameters.
First, create 4 files to experiment with.
touch a.js b.js c.js d.js
Then run the following command:
ls|xargs -t -n2 ls -al
The output is as follows, -n2 indicating that the parameters are set in groups of 2 and passed to the following command.
ls-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.jsls-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.js
Example: Special file name
Sometimes, a file name may have special characters, such as a space in the file name below.
touch 'hello 01.css' 'hello 02.css'
The previous command will cause an error because it xargs is separated by a space/line break, and the expected behavior occurs.
# 命令find'*.css'|xargs -t ls -al#输出ls# 展开后的命令ls: cannot access ./hello: No such file or directoryls: cannot access 01.css: No such file or directoryls: cannot access ./hello: No such file or directoryls: cannot access 02.css: No such file or directory
xargsThis is the way to solve the problem.
-print0: tells the find command, after the output file name, to follow the NULL character, not the line break;
-0: Tell xargs , NULL as a parameter delimiter;
find'*.css'|xargs -0 -t ls -al
Example: Log backup
Back up logs from 7 days ago to a specific directory
find|xargs'{}'{} /tmp/otc-svr-logs/
RELATED LINKS
Https://craftsmanbai.gitbooks.io/linux-learning-wiki/content/xargs.html
Http://wiki.jikexueyuan.com/project/shell-learning/xargs.html
Linux Basics: Xargs command