Detailed xargs

binding xargs find

duct is to pass the output of a command as input to another command, for example: command1 | command2 command2 but only the contents of the output parameters as the input character string,
you can not take it as a file handle.
xargs is to be able to operate the search to find documents written. It came as a pipe string can file subsequent to the execution of the command.

[root@localhost ~]# find /etc/ -name "hosts" | cat
/etc/hosts
[root@localhost ~]# find /etc/ -name "hosts" | xargs cat
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

Note:
When using the -exec option to deal with the find command to match the file, find the command will be passed along to all matching files to execute exec. However, some systems can be passed to exec
limited command length, so that after the find command to run a few minutes, an overflow error occurs, the error message is usually "argument list too long" or "parameter column overflow." This is xrags
useful command is located, especially when combined with the find command. find command to match the file transfer command to the xargs, and xargs command each get only part of the file rather than the
whole, not as -exec option. So that it can first deal with the first part of the file retrieved, and then the next batch, and so continue.
In some systems, using -exec option to process each matching file to initiate a corresponding process, not the entire file to match the first performance as an argument; it is a
case some cases there will be too many processes, system performance the problem of declining. So efficiency is not high; but with the xargs command is only one process. In addition, when using the xargs command, what
is the time to get all the parameters, or the parameters obtained in batches, and each obtained digital parameters will be determined based on the options of the command and the corresponding kernel tunable parameters.
find | xrags command

Comparing the difference between the -exec and xargs

Example 1: Verify implementation

[root@localhost tmp]# touch {1..10}.txt
[root@localhost tmp]# find -name "*.txt" -exec ls {} \; 
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
./10.txt
[root@localhost tmp]# find -name "*.txt" | xargs ls
./10.txt ./1.txt ./2.txt ./3.txt ./4.txt ./5.txt ./6.txt    ./7.txt ./8.txt ./9.txt

 

Example 2: Verification efficiency

[root@localhost tmp]# find . -name "*.txt" -exec rm {} \;
[root@localhost tmp]# touch {1..100000}.txt
[root@localhost ~]# find -name "*.txt" | xargs rm

 


xargs common options

@ -0, when sdtin special characters, which as a general character, image / space character 
[the root @ localhost ~] # Touch  " ab & C.txt " 
[the root @ localhost ~] # Find -name " * .txt " | xargs - 0  LS 
LS : can not access ./ ab c.txt
: No such file or directory
[root@localhost ~]# find -name "*.txt" -print0 | xargs -0 ls
./a b c.txt

 

// -a File is read from a file as stdin 
[the root @ localhost ~] # CAT > . 1 .txt << the EOF
 > AAA BBB
 > CCC ddd
 > ASD ER
 > the EOF
[root@localhost ~]# xargs -a 1.txt
asdasasd asdasdasd ASD

 

// Sometimes -e flag, attention may be -E, flag separated by a space must be a sign, when xargs their analysis contains flag this flag stops 
[root @ localhost ~] # xargs -E ' ddd ' -a 1 .txt
aaa bbb ccc
[root@localhost ~]# cat 1.txt | xargs -E 'ddd'
aaa bbb ccc

 

// add back the number -n num, represents the number of command at the time of execution time with the argument, the default is to use all the 
[root @ localhost ~] # CAT  1 .txt | xargs -n 2
aaa bbb
ccc ddd
a s
there

 

// -p operations have interactivity, each execution of the command will prompt the user to interactively select, every time a argument when asked to perform a user 
[root @ localhost ~] # CAT  1 .txt | xargs - the p-
 echo aaa bbb ER asd ddd ccc? ... the y-
aaa bbb ccc ddd a s d er

 

// -i or -I, it depends linux support, each of the item names xargs is generally assigned to the line by line, {}, {} may be used instead of 
[the root @ localhost ~] # Touch { 2 .. 10 }.TXT
#method one
[root@localhost ~]# find -name "*.txt" | xargs -t cp -t /tmp/
#Method Two
[root@localhost ~]# cp -v `find /root -name "*.txt"` /var/tmp/
[root@localhost ~]# cp $(find /root -name "*.txt") /var/tmp/
# Method three
[root@localhost ~]# find -name "*.txt" -exec cp {} /tmp \;
# Method Four
[root@localhost ~]# find -name "*.txt" | xargs -I {} cp -v {} /var/tmp/
"./a b c.txt" -> "/var/tmp/a b c.txt"
"./1.txt" -> "/var/tmp/1.txt"
"./2.txt" -> "/var/tmp/2.txt"
"./3.txt" -> "/var/tmp/3.txt"
"./4.txt" -> "/var/tmp/4.txt"
"./5.txt" -> "/var/tmp/5.txt"
"./6.txt" -> "/var/tmp/6.txt"
"./7.txt" -> "/var/tmp/7.txt"
"./8.txt" -> "/var/tmp/8.txt"
"./9.txt" -> "/var/tmp/9.txt"
"./10.txt" -> "/var/tmp/10.txt"

Guess you like

Origin www.cnblogs.com/xmtxh/p/12098422.html