What is the xargs command for?

0

What is the xargs ?

For example, I've seen examples

 ls pasta/ | xargs -l git update-index

I saw that this was being used because the git update-index command does not work with folders.

But in practice, I do not know what this command is for.

    
asked by anonymous 21.03.2016 / 20:38

1 answer

9

The primary function is to build parameter lists and pass it on to other programs or instructions. This command should be used as follows:

xargs [comando [argumento inicial]]

If the command, which can be included in a shell script, is omitted, echo will be used by default.

The xargs combines the initial argument with the arguments received from the default entry, in order to execute the specified command one or more times.

Example:

Let's look in all files below a given directory for a string using the find command with the type f option to search only the normal files, looking for directories, special files, links files, etc., the most generic being the name of the home directory and the string to be searched as parameters. For this we do:

$ cat grepr
  #
  # Grep recursivo
  # Pesquisa a cadeia de caracteres definida em $2 a partir do diretorio $1
  #
  find $1 -type f -print|xargs grep -l "$2"

In the execution of this script we look for, from the directory defined in the $ 1 variable, all the files that contained the string defined in the $ 2 variable.

Exactly the same thing could be done if the program line were as follows:

find $1 -type f -exec grep -l "$2" {} \;

This process has two major disadvantages over the previous one:

  • The first is quite visible: the execution time of this method is much higher than that, because the grep will be done in each file that is passed through the find, one-by-one, while with the xargs, it will be all over, or in the worst case, most of the list of files generated by find;

  • Depending on the number of files found that meet the find, we can win that famous and fateful error message "Too many arguments" indicating an execution stack overflow grep. As stated in the previous item, if we use xargs it will pass for grep as many parameters as possible, enough not cause this error, and if necessary will execute grep more at once.

  • ATTENTION! Here are some linux people who use% color% as a dye port: In the following examples that involve this statement, you should use the ls option, otherwise there are high chances that the results will not occur as expected. >

    Let's now look at an example that is more or less the reverse of this one we just saw. This time, we will make a script to remove all the files of the current directory, belonging to a certain user.

    The first idea that comes up is, as in the previous case, to use a find command, as follows:

    $ find . -user cara -exec rm -f {} \;
    

    It would almost be right, the problem is that this way you would remove not only the files from the guy in the current directory, but also from all the other subdirectories "hung" on this one. Let's see how to do it:

    $ ls -l | grep " cara " | cut -c55- | xargs rm
    

    In this way, grep selected the files that contained the expensive string in the current directory listed by ls -l. The cut command took only the name of the files, passing them to removal by rm using the xargs command as a bridge.

    Font

        
    21.03.2016 / 21:18