1. Introduction
This command is available because many Linux commands do not support piping parameters, such as
Find/sbin-perm +700 | Ls-l This command is wrong.
Find/sbin-perm +700 |xargs Ls-l This is the right thing.
Normally the Linux command can be |
connected to the end, and the previous command's stdout is connected to the stdin of the next command. However, some commands, such as, and ls
rm
so on, are accepted from the command line parameters. This is not a good time to pass the output of the previous command to them. So there it is xargs
.
In a nutshell, xargs
you can separate the input from the stdin, either by a blank or add-on character, and then sequentially as a parameter to invoke xargs
the following command
Xargs default delimiter: A space or carriage return
2. Parameter usage
Want to delete all the .jpg
files, of course you can rm *.jpg
, but if you want to recursively manipulate all the files under subdirectories?
Can do this:
find . -name "*.jpg" | xargs rm
In this way, all find
the found file names will be invoked as arguments to the rm
command.
For the most part, this line of command is fine, but if some filenames contain spaces, there is a problem.
xargs
By default, the accepted input is delimited by a whitespace character, so a file name with a space will be passed as multiple parameters, respectively rm
. So when dealing with commands such as file names, this is usually the case:
find . -name "*.jpg" -print0 | xargs -0 rm
Here -print0
is the tell find
command, after each output ‘\0‘
as end. -0
is xargs
to tell, use ‘\0‘
to separate the input, not the whitespace character. This avoids any problems.
Consider another scenario below, assuming that you are not deleting, but want to add a suffix to the required file names .bak
? This is what you need:
find . -name "*.jpg" -print0 | xargs -0 -I {} mv {} {}.bak
One -I {}
is to tell xargs
that the following command, with the {}
representation placeholder, will be replaced by the actual parameter. That's all.
Other useful parameters are:
-n
Used to specify that several parameters are passed each time
-d
Used to specify when to slice the input, the specific delimiter
For more parameters, refer to man xargs
it.
- -A file is read from files as Sdtin
$ cat 1.txt
AAA BBB CCC DDD
A b
$ xargs-a 1.txt Echo
AAA BBB CCC DDD A B
- -e flag, note that sometimes-e,flag must be a space-delimited flag that stops when Xargs analyzes the flag that contains flag
$ Xargs-e ' ddd '-a 1.txt echo
AAA BBB CCC
$ cat 1.txt |xargs-e ' DDD ' echo
AAA BBB CCC
- -N num followed by the number of times, indicating the number of times the command was executed at the time of execution, argument by default.
$ cat 1.txt |xargs-n 2 echo
AAA BBB
CCC DDD
A b
- The-p operation is interactive, with each execution comand interactively prompting the user to select, asking the user every time a argument is executed
$ cat 1.txt |xargs-p Echo
Echo AAA BBB CCC ddd a b?... y
AAA BBB CCC DDD A B
$ cat 1.txt |xargs-p Echo
Echo AAA BBB CCC DDD a b?... n
- -T means that the command is printed before execution.
$ cat 1.txt |xargs-t Echo
Echo AAA BBB CCC DDD A B
AAA BBB CCC DDD A B
- -I or-I, which depends on Linux support, will be the xargs of each name, usually a line assigned to {}, you can use {} instead.
$ ls
1.txt 2.txt 3.txt Log.xml
$ ls *.txt | XARGS-T-i mv {} {}.bak
MV 1.txt 1.txt.bak
MV 2.txt 2.txt.bak
MV 3.txt 3.txt.bak
$ ls
1.txt.bak 2.txt.bak 3.txt.bak Log.xml
Note that-I must specify the replacement character-i specifies the substitution character-optional, for example:
Find. | Xargs-i {} CP {} $D _path
And
Find. | Xargs-i CP {} $D _path
Note: In Cshell and Tcshell, you need to enclose {} in single quotation marks, double quotes, or backslashes, otherwise you do not know. Bash can be used without.
Find/shell-maxdepth 2-name A-print | Xargs-t-I sed-i ' 1 i\111 ' {} '
- -R No-run-if-empty If there are no parameters to process to Xargsxargs by default is with empty parameters run once, if you want no parameters, stop Xargs, direct exit, use the-r option, it can prevent xargs command with empty parameters run error.
$ echo "" |xargs-t MV
Mv
mv:missing file operand
Try ' MV--help ' for more information.
$ echo "" |xargs-t-R MV #直接退出
-S NUM xargs the maximum number of command-line characters (with spaces) behind that command
$ cat 1.txt.bak |xargs-s 9 Echo
Aaa
Bbb
Ccc
Ddd
A b
$ cat 1.txt.bak |xargs-s 4 echo
Xargs:can not fit single argument within argument list size limit #length (echo) =4
$ cat 1.txt.bak |xargs-s 8 Echo
Xargs:argument line too long #length (echo) =4,length (AAA) =3,length (NULL) =1,total_length=8
- -L reads NUM lines once from standard input to command commands, like the-L and-l functions
$ cat 1.txt.bak
AAA BBB CCC DDD
A b
Ccc
DSDs
$ cat 1.txt.bak |xargs-l 4 echo
AAA BBB CCC DDD a B CCC DSDS
$ cat 1.txt.bak |xargs-l 1 Echo
AAA BBB CCC DDD
A b
Ccc
DSDs
The-D Delim delimiter, the default Xargs delimiter is carriage return, the delimiter for argument is a space, and the Xargs delimiter is modified here
$ cat 1.txt.bak
[Email protected] BBB [email protected] ddd
A b
$ cat 1.txt.bak |xargs-d ' @ ' echo
AAA BBB CCC DDD
A b
-X exit means that if there is any command line larger than the-s Size flag specified by the number of bytes, stop running the xargs command,-l-i-n defaults to the-x parameter, mostly with-s used
-P modifies the maximum number of processes, the default is 1, which is 0 times for as many as it can.
3. Xargs and find
When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command. The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.
In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases, there are too many processes and degraded system performance, so the efficiency is not high , while using the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.
The pipeline is to pass the output of one command to another command as input, such as: Command1 | Command2 but Command2 only takes the output as an input parameter. Find. -name "Install.log"-print print Out is Install.log this string, if only uses the pipeline, then Command2 can use only install.log this string, cannot treat it as a file.
Of course this command2 except Xargs. Xargs is written to be able to manipulate the files found by find. It can send a string from the pipeline as a file to the subsequent command execution.
For example:
$find.-name "Install.log"-print | Cat
./install.log #显示从管道传来的内容, just as a string to handle the
$find.-name "Install.log"-print | xargs cat
AAAAAA and nbsp #将管道传来的内容作为文件 for cat execution. In other words, the command executes if there is a install.log, then prints out the contents of the file.
Take a look at how the Xargs command is used with the Find command, and give some examples.
1, in the current directory to find all users have read, write and Execute permissions files, and recover the corresponding write permissions:
# Find. -perm-7-print | Xargs chmod o-w
2. Find every normal file in the system, and then use the Xargs command to test what type of file they belong to
# Find. -type F-print | Xargs file
./liyao:empty
3, try to delete too many files with RM, you may get an error message:/BIN/RM Argument list too long. Use Xargs to avoid this problem.
$find ~-name ' *.log '-print0 | Xargs-i-0 Rm-f {}
4. Find all the JPG files and compress it
# Find/-name *.jpg-type F-print | Xargs Tar-cvzf images.tar.gz
5, copy all the picture files to an external hard drive
# ls *.jpg | Xargs-n1-i CP {}/external-hard-drive/directory
Reference to: http://blog.csdn.net/zhangfn2011/article/details/6776925
http://blog.csdn.net/hittata/article/details/8021500
Http://www.cnblogs.com/wdpp/archive/2012/02/28/2386683.html
http://blog.163.com/ly_89/blog/static/18690229920117208314257/
Http://aix.chinaunix.net/doc/2008/03/01/1108340.shtml
Http://blog.chinaunix.net/uid-15117916-id-4920.html
Xargs principle & Use