pipelines are implemented " The previous standard output is used as the following standard input "
Xargs is to achieve " to use standard input as a parameter to a command "
Contrast:
Ind/sbin-perm +700 |ls-l
Find/sbin-perm +700 |xargs ls-l
Parameters and Usage:
-0: When the Sdtin contains special characters, it is treated as a general character, such as "space", etc.
# echo "//" |xargs-0 echo > aaa# echo "//" |xargs echo > bbb# cat-a aaa//$$# cat-a bbb//$
-A file is read from files as Sdtin
$ cat>1.txt<<eof AAA BBB CCC Ddda beof$ xargs-a 1.txt echoaaa 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.
#使用小e参数, there will be waiting input, it is recommended to use large E
$ Xargs-e ' ddd '-a 1.txt echoaaa bbb ccc$ cat 1.txt |xargs-e ' DDD ' echoaaa 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 echoaaa bbbccc Ddda b
#如果将所有filed排成一列
$ cat 1.txt |xargs-n 1
-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 mvmv mv:missing file operandtry ' mv--help ' for more information.$ echo "" |xargs-t-R MV #直接退 Out
-S NUM xargs the maximum number of command-line characters (with spaces) behind that command
$ cat 1.txt.bak |xargs-s 9 Echoaaabbbcccddda b$ cat 1.txt.bak |xargs-s 4 Echoxargs:can not fit single argument within argument list size limit #length (echo) =4$ cat 1.txt.bak |xargs-s 8 echoxargs:argument line too long #length ( echo) =4,length (AAA) =3,length (NULL) =1,total_length=8
-I or-I, which depends on Linux support, will be the xargs of each name, usually a line assigned to {}, you can use {} instead.
$ ls1.txt 2.txt 3.txt log.xml$ ls *.txt |xargs-t-i mv {} {}.bakmv 1.txt 1.txt.bak mv 2.txt 2.txt.bak MV 3.txt 3.txt.b AK $ ls1.txt.bak 2.txt.bak 3.txt.bak log.xml
Note that-I must specify the replacement character-i specifies the substitution character-optional
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 ' {} '
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 Echoecho AAA BBB CCC ddd a B?... yaaa BBB CCC ddd a b$ cat 1.txt |xargs-p echoecho AAA BBB CCC DDD A b?... n
-T means that the command is printed before execution.
$ cat 1.txt |xargs-t Echoecho AAA BBB CCC DDD a B AAA BBB CCC DDD A B
-L reads NUM lines once from standard input to command commands, like the-L and-l functions
$ cat 1.txt.bak AAA BBB CCC Ddda bcccdsds$ cat 1.txt.bak |xargs-l 4 echoaaa BBB CCC DDD a B CCC dsds$ cat 1.txt.bak |xar Gs-l 1 echoaaa BBB CCC Ddda BCCCDSDS
The-D delimter 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] Ddda b$ cat 1.txt.bak|xargs-d ' @ ' echoaaa BBB CCC Ddda b
- x Exit meaning that if there is any Command row greater than - s Size flag Specifies the number of bytes to stop running Xargs command, -l-i-N Open By default - x parameters, mainly with the - S Use
- P Modify the maximum number of processes, default is 1 , for 0 Time for As Manyas it can .
Instance Usage :
find . -perm -7-print | xargs chmod o-w #在当前目录下查找所有用户具有读, write and Execute permissions files, and reclaim the appropriate write permissions find . -type f-print | xargs file #查找系统中的每一个普通文件, and then use the Xargs command to test what kind of file they belong to Find ~ -name ' *.log ' -print0 | xargs -i -0 rm -f {} #rm删除文件太多 to avoid/bin/rm argument list too long errors find / -name *.jpg-type f -print | xargs tar -cvzf images.tar.gz #查找所有的jpg file, and compress it ls *.jpg | xargs-n1 -i cp {} /external-hard-drive/ directory #拷贝所有的图片文件到一个外部的硬盘驱动find / - Perm +7000 | xargs ls -l # the previous content as the parameter of the following command seq 1 10 |xargs -i date -d "{} days " +%y-%m-%d # lists 10 day dates Seq 1 10 |xargs -i touch "{}.txt" #一次生成10个空文件cat a.txt | xargs # Career cat a.txt | xargs -n1 # Row to column find .| Xargs grep -ri "192.168.1.112" #找出目下下所有文件包含192.168.1.112 characters ssh -n $ip ' find ' $path ' /data /opt -typef -name "*.sh" -or -name "*.py" -or -name "*.pl" |xargs tar zcvpf /tmp/data_Backup.tar.gz ' #远程打包
Reference:
http://czmmiao.iteye.com/blog/1949225
http://blog.csdn.net/yhcharles/article/details/44875865
http://blog.csdn.net/zhangfn2011/article/details/6776925/
http://blog.csdn.net/yongan1006/article/details/8134581
http://czmmiao.iteye.com/blog/1949225
Http://www.cnblogs.com/perfy/archive/2012/07/24/2606101.html
[Boiled Water]-shell-xargs knowledge summary-Knowledge point