xargs principle & use

1 Introduction

  The reason to use this command, because many linux command does not support passing parameters pipelines, such as

  find / sbin -perm +700 | ls -l command is wrong

  find / sbin -perm +700 | xargs ls -l it means is that

  Typically Linux command can be used |end to end, on a command stdout connected to the next command stdin. But some commands, such as ls, rmetc., from the command line parameter accepts input. If you want to output this time a command passed to them, harder to resolve. So there xargs.

  Briefly, xargscan receive input from stdin, or by adding a carriage whitespace separated, as a parameter to call followed by xargssubsequent commands

  xargs default separator: space or carriage return

2. Use parameter

  Want all .jpgdeleted files, of course you can  rm *.jpg, but if you want to manipulate files recursively in all subdirectories it?

  It can be:

  • find . -name "*.jpg" | xargs rm

   In this way, all the findfile names found will be invoked as an argument rmcommands

    For the most part, the command line is no problem, but some, such as the file name contains spaces, there will be a problem.

   xargsThe default blank delimited input received, the name of a file containing a plurality of spaces are used as parameters, they are passed rm. So when dealing with file names such orders, usually this:

  • find . -name "*.jpg" -print0 | xargs -0 rm

       Here  -print0 it is to tell findthe command to the back of each output '\0'as the end. -0We are told xargsto use '\0'to separate input rather than whitespace. This avoids the problem arises.

      Here again consider another case, the assumption is not deleted, but wanted to meet the requirements of the file name suffixes are added on .bakhow to do? This time need:

  • find . -name "*.jpg" -print0 | xargs -0 -I {} mv {} {}.bak

  Which -I {}is to tell xargs, the latter command, a {}placeholder will be replaced by the actual parameters. that's fine.

  Other useful parameters are: 
  -n specifies several parameters on each pass 
  -d during slicing is used to specify the input content, specific delimiter 
  other more parameters, reference to man xargsit.

  • -a file from the file reads 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 some time may be -E, flag must be separated by a space flag, when xargs analysis to contain flag when the flag is stopped

    $ xargs -E 'ddd'  -a 1.txt echo
    aaa bbb ccc

    $ cat 1.txt |xargs -E 'ddd' echo
    aaa bbb ccc

  • -n num plus the number back, represents the number of command at the time of execution time with the argument, the default is all.

    $ cat 1.txt |xargs -n 2 echo
    aaa bbb
    ccc ddd
    a b

  • -p operation has interactivity, each execution comand all interactive prompts the user to select, when you perform one argument when a user asks

    $ 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 represents the first print command, and then execute.

    $ cat 1.txt |xargs -t echo
    echo aaa bbb ccc ddd a b 
    aaa bbb ccc ddd a b

  • -i or -I, it depends linux support, each of the xargs name, is generally assigned to the line by line, {}, {} may be used 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, -I must specify a replacement character -i Specifies whether to replace characters - optional, for example:
    the Find | xargs cp the -I {} {} $ D_PATH.
    And
    find | xargs -i cp {} $ D_PATH.

  Note: cshell and tcshell in {} it is necessary to single and double quotation marks or backslash, or do not know. bash can not.
  find / shell -maxdepth 2 -name a -print | xargs -t -i sed -i '1 i \ 111' '{}'

  • -r no-run-if-empty if there is no argument to be processed is passed to xargsxargs default parameters with empty run once, if you want no parameters, stop xargs, exit, you can use the -r option, which can prevent xargs after the command to run with empty parameter error.

    Echo $ "" | xargs -t mv
    mv 
    mv: File Missing operand
    . The Try `mv --help 'for More Information
    $ echo" "| xargs -t -r # mv exit
    behind num xargs -s command that command the greatest the number of lines of characters (including spaces) 
    $ CAT 1.txt.bak | xargs echo -s 9
    aaa
    bbb
    ccc
    ddd
    ab
    $ CAT 1.txt.bak | xargs echo -s 4
    xargs: CAN not Fit the WITHIN SINGLE argument argument List size limit #length (echo). 4 =
    $ CAT 1.txt.bak | xargs echo -s. 8
    xargs: TOO Long Line argument #length (echo) =. 4, length (AAA) =. 3, length (null) =. 1, total_length = 8

  • -L num rows read from a standard input command to the Command, -l, -L function as

    CAT 1.txt.bak $ 
    AAA BBB CCC ddd
    ab &
    CCC
    DSDS
    $ CAT 1.txt.bak | xargs -L echo. 4
    AAA BBB CCC CCC ddd ab & DSDS
    $ CAT 1.txt.bak | xargs echo. 1 -L
    AAA BBB CCC ddd
    ab &
    CCC
    DSDS
    -d the delim delimiter, the default delimiter xargs carriage return, argument separator is a space, where the modified separator is xargs

    $ cat 1.txt.bak 
    aaa@ bbb ccc@ ddd
    a b

    $ cat 1.txt.bak |xargs  -d '@' echo
    aaa  bbb ccc  ddd
    a b

    -x Exit means, if any row is greater than Command -s Size flag specifies the number of bytes, stop running xargs command, -L -I -n -x open default parameters, primarily with the use -s
    -P modified maximum the number of processes, the default is 1, 0 is when as many as it can.

 

3. xargs和find

  When using find -exec command options to deal with the matching file, find the command passed along to all matching files to execute exec. However, some systems have limitations on the length of the command can be transmitted to the exec, so after the find command to run a few minutes, an overflow error occurs. Error message is usually "argument list too long" or "parameter column overflow." This is useful where the xargs command, 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, but not all, unlike the -exec option that. 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 is not matched to the file all at once executed as a parameter; so in some cases there will be too many processes, system performance degradation problems, which is not efficient; and the use xargs command is only one process. In addition, when using the xargs command, whether it is the time to get all the parameters, or in batches to obtain parameters, acquisition parameters and the number of each of the options will be determined based on the command and the corresponding kernel tunable parameters.

Pipe 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 as an input parameter. find. -name "install.log" -print print out this string is install.log, if only to use the pipe, then command2 can use only install.log string, can not take it as a file to be processed .

Of course, this command2 addition to xargs. xargs is to be able to operate the search to find documents written. It can pipe string came as a command file to subsequent implementation.
For example:
. $ The Find -name "install.log" -print | CAT
./install.log # display content coming from the pipe, just as strings to deal with
$ find -name "install.log" -print. | xargs cat
aaaaaa # the pipeline came as the file contents, to perform cat. That is, the command is executed if the install.log exist, then print out the contents of the file.
Take a look at how xargs command is used with the find command, and give some examples.

1, look in the current directory of all files the user has read, write, and execute permissions, and recover the corresponding write permissions:
. # -Perm the Find -7 -print | xargs chmod OW
every 2 ordinary file, look for the system, then use xargs command to test what kind of file they belong to
# the Find The -type f -print | xargs file.
./liyao: empty

3, try to remove too much of a rm file, you may get an error message: / bin / rm Argument list too long with xargs to avoid this problem.
$ The Find ~ -name '* .log' -print0 | xargs -i - 0 rm -f {}

4, find all jpg files, and compress it
# the Find / -name * .jpg The -type f -print | xargs tar -cvzf images.tar.gz
5, copy all the image files to an external hard drive 
# ls * .jpg | xargs -n1 -i cp {} / external-hard-drive / directory

 

Reference: https: //www.cnblogs.com/yorkyang/p/6294879.html

Guess you like

Origin www.cnblogs.com/yoyowin/p/12066935.html