Execute command : Xargs
When find produces a list of files , It is sometimes useful to provide the list to another command .
Case :
$touch abc.c ERD.C oiy.c
$ll
./erd.c
./abc.c
./oiy.c
$find-name ' *.c ' | Rm
RM: missing operand
Please try "rm--help" for more information.
$find-name ' *.c ' | Xargs RM
$find-name ' *.c '
Without any display , the description was successfully deleted .
1. Introduction , the key to this command is that many commands do not support pipelines (|) to pass parameters , and it's necessary in daily work. , so there it is. Xargs Command , Above Example .
Xargs can read into the stdin data , and with a blank or a break in the cell as a resolution , will stdin data is separated into atguments because it is separated by a blank element , so , If there are some filenames or other meaningful nouns that contain white space , the Xargs may appear to be mistaken .
$touch ' file 1.log ' file 2.log '
$ll
Total dosage 0
-rw-r--r--1 root root 0 7 months 10:18 file 1.log
-rw-r--r--1 root root 0 7 months 10:18 file 2.log
$find-name ' *.log '
./file 2.log
./file 1.log
$find-name ' *.log ' | Xargs RM
RM: Cannot delete "./file": no file or directory
RM: Unable to delete "2.log": no file or directory
RM: Cannot delete "./file": no file or directory
RM: Unable to delete "1.log": no file or directory
The reason is simple , Xargs the default is to split the record with whitespace characters ( spaces , tabs, line breaks ) , so the file name ./file 1.log was interpreted as two records. ./file and the 1.log, Unfortunately, RM I can't find these two files . .
To solve this kind of problem,Smart man came up with a way,LetFindafter printing out a file name, then output aNULLcharacter(‘ ’)Instead of a newline character.,and then I'll tellXargsalso useNULLcharacter as the delimiter for the record,this isFindof the-printand theXargsof the-0Options.
$find-name ' *.log '-print0 | xargs-0 RM
2. Main options
Options |
Meaning |
-0 |
When stdin contains special sub-elements , It is treated as a general character |
-A file |
Read in file as stdin |
-E Flag |
Note that sometimes it may be -e,flag must be a space-delimited flag , when xargs is analyzed to contain flag Stop when this sign is on. |
-P |
Ask the user once every time a argument is executed . |
-N num |
The number of times added , indicating that the command executes at the time of the arguments number , By default is used all . |
-T |
is to print the command first and then execute the |
-I. |
Or- I, the name of each item in the xargs , usually a row of the assignment to {}, you can use {} instead of |
-R No-run-if-enpty |
When the input of the Xargs is empty, it stops Xargs and does not need to be executed again. |
-S NUM |
Maximum number of characters on the command line |
-D Delim |
Delimiter , the default xargs delimiter is carriage return , argument delimiter is a space , here is modified delimiter for Xargs |
-X |
Exit means that the main match - s is used |
-P |
Modify the maximum number of processes, default is 1, 0 when as Mang as it can |
the difference between find-print and -print0 :
-print a carriage return newline character is added after each output , and -print0 does not .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Shell Learning 45 days----Xargs