What is the Xarg command for?

Asked

Viewed 5,223 times

1

What is the command for xargs?

For example, I’ve seen examples

 ls pasta/ | xargs -l git update-index

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

But in practice, I don’t know what that command is for.

  • 3

    People who are negatively, it would be interesting to point to the author what makes the question deserving of so many negatives, so you collaborate with the quality of the site and the learning of OP.

1 answer

12


The primary function is to construct lists of parameters and pass it on to the execution of other programs or instructions. This command must be used as follows:

xargs [comando [argumento inicial]]

If the command, which can be including a Shell script, is omitted, echo will be used by default.

The xargs combines the initial argument with the arguments received from the standard input in order to execute the specified command once or more.

Example:

Let’s search in all the files under a given directory a string using the find command with type f option to search only the normal files, discarding directories, special files, link files, etc, and we will make it more generic by getting the name of the initial directory and the string to be searched as parameters. For that 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, from the directory defined in variable $1, all the files that contained the string defined in variable $2.

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

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

This process has two major disadvantages on the previous:

  1. The first is quite visible: the execution time of this method is far superior to that, because the grep will be made in each file that is passed to you by find, one-to-one, while with the xargs, will be passed all, or at worst, most possible, from the list of files generated by find;

  2. Depending on the amount of files found that meet the find, we might win that famous and fateful error message "Too Many Arguments" indicating an overflow of the execution stack of grep. As stated in the previous item, if we use the xargs it will pass for grep as many parameters as possible, sufficient not to cause this error, and if necessary will perform the grep more at once.

ATTENTION! There are some linux people who use ls color that neither dyeing door: in the following examples that involve this instruction, you should use the option --color=none, otherwise there are high chances that the results will not occur as expected.

Let us now look at an example that is more or less the opposite of the one we have just seen. This time, we will make a script to remove all files from the current directory, belonging to a particular user.

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

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

Almost would be right, the problem is that in this way you would remove not only the guy’s files in the current directory, but also from all the other "hanging" subdirectories in 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 for removal by rm using the xargs bridge command.

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.