Xargs usage Explanation
1. Introduction
The key to using this command is that many commands do not support | Pipelines to pass parameters, and in daily work there is a need for the Xargs command, for example:
Find/sbin-perm +700 |ls-l This command is wrong.
Find/sbin-perm +700 |xargs Ls-l This is the right thing.
Xargs can read into the stdin data, and with blank characters or word-breaking characters as a resolution, the stdin data is separated into arguments. Because it is separated by a blank character, so, if there are some file names or other meanings of nouns containing empty white space, Xargs may be misjudged ~ His usage is actually also full of simple! Just take a look at it first!
2. Option explanation
-0 when Sdtin contains special characters, think of it as a general character, like/' spaces, etc.
For example: [email Protected]:~/test#echo "//" |xargs Echo
[Email Protected]:~/test#echo "//" |xargs-0 Echo
/
-A file is read from files as Sdtin, (see example I)
-e flag, note that sometimes-e,flag must be a space-delimited flag that stops when Xargs analyzes the flag that contains flag. (Example II)
-P asks the user once every time a argument is executed. (Example III)
-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. (Example IV)
-T means that the command is printed before execution. (Example V)
-I or-I, which depends on Linux support, will be the xargs of each name, usually a line assigned to {}, you can use {} instead. (Example VI)
-R no-run-if-empty Stop Xargs when the input of Xargs is empty, no more execution. (Example VII)
The best number of characters for the-S NUM command line, which refers to the maximum number of command-line characters for the command after Xargs. (Example VIII)
-L num Use at most max-lines nonblank input lines per command line.-s contains spaces.
-L and-l
The-D Delim delimiter, the default Xargs delimiter is carriage return, the delimiter for argument is a space, and here the Xargs delimiter is modified (example nine)
-X exit means mainly with-s used.
-P Modify the maximum number of processes, the default is 1, for 0 times as many as it can, this example I did not think, should normally not be used.
3. Application examples
Example one:
[Email Protected]:~/test#cat test
#!/bin/sh
echo "Hello world/n"
[Email protected]:~/test#xargs-a Test echo
#!/bin/sh echo Hello world/n
[Email protected]:~/test#
Example two:
[Email Protected]:~/test#cat txt
/bin Tao Shou Kun
[Email protected]:~/test#cat txt|xargs-e ' Shou ' echo
/bin Tao
[Email protected]:~/test#
Example three:
[Email Protected]:~/test#cat txt|xargs-p Echo
Echo/bin Tao Shou kun ff?... y
/bin Tao Shou Kun ff
Example four:
[Email Protected]:~/test#cat txt|xargs-n1 Echo
/bin
Tao
Shou
Kun
[Email Protected]:~/test3#cat Txt|xargs Echo
/bin Tao Shou Kun
Example five:
[Email Protected]:~/test#cat txt|xargs-t Echo
Echo/bin Tao Shou Kun
/bin Tao Shou Kun
Example VI:
$ ls | XARGS-T-i mv {} {}.bak
Example VII:
[Email Protected]:~/test#echo "" |xargs-t MV
Mv
mv:missing file operand
Try ' MV--help ' for more information.
[Email Protected]:~/test#echo "" |xargs-t-R MV
[Email protected]:~/test#
(Direct exit)
Example VIII:
[Email protected]:~/test#cat test |xargs-i-x-s echo ' {} '
Exp1
Exp5
File
Xargs:argument Line too long
Linux-2
[Email protected]:~/test#
Example nine:
[Email protected]:~/test#cat txt |xargs-i-p echo {}
Echo/bin Tao Shou kun?... y
[Email protected]:~/test#cat txt |xargs-i-p-d "" Echo {}
Echo/bin?... y
Echo Tao?.../bin
Y
Echo Shou?... tao
Again such as:
[Email protected]:~/test#cat test |xargs-i-p-d "" Echo {}
Echo EXP1
Exp5
File
Linux-2
Ngis_post
Tao
Test
Txt
Xen-3
?... y
[Email protected]:~/test#cat test |xargs-i-p echo {}
echo Exp1?... y
echo EXP5?... EXP1
Y
echo File?... exp5
Y
The-i parameter of args
Args and find often combined to carry out file operations, usually delete the log when it is only customary to remove, such as # Find. -type f-name "*.log" | XargsRM-RF * will delete the file ending with log, if I want to move or copy it, I need to use parameters instead. The Xargs-i parameter or the-I parameter with {} can be used for file operation. -I. REPLACE-STR
Replace occurrences of Replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate
Input items; Instead the separator is the newline character. Implies-x and-l 1.
--REPLACE[=REPLACE-STR],-i[replace-str]
This option was a synonym For-ireplace-str if replace-str is specified, and for-i{} otherwise. This option is deprecated; Use-i instead.
Man took a moment to see or not quite understand, through examples, contrived experiment to write my understanding. ############### files under directory ###############[[email protected] ab]# ls
1kk.Zip3kk.zip 5kk.zip b.rar d.rar f.rar h.rar j.rar mini.txt ni.txt
2kk.zip 4kk.zip a.rar c.rar e.rar g.rar i.rar k.rar nii.txt################## #使用 i parameter ##################
[[email protected] ab]# find. -type f-name "*.txt" | Xargs-iCP{}/tmp/k/
[email protected] ab]# ls. /k/
Mini.txt Nii.txt Ni.txt
[[email protected] ab]# ################### use I parameter ################
[[email protected] ab]# find. -type f-name "*.txt" | Xargs-i {} CP {}/tmp/n/
[email protected] ab]# ls. /n/
Mini.txt nii.txt ni.txt results come out, plus-I parameter directly with {} can replace the standard output of the pipeline before the content;
The plus-I parameter requires the substitution character to be specified beforehand
Xargs usage details (RPM)