Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use xargs when piping?

Tags:

bash

xargs

I am new to bash and I am trying to understand the use of xargs, which is still not clear for me. For example:

history | grep ls 

Here I am searching for the command ls in my history. In this command, I did not use xargs and it worked fine.

find /etc - name "*.txt" | xargs ls -l 

I this one, I had to use xargs but I still can not understand the difference and I am not able to decide correctly when to use xargs and when not.

like image 951
Sara Hamad Avatar asked Feb 23 '16 22:02

Sara Hamad


People also ask

What does piping to xargs do?

The xargs utility allows you to build and execute commands from standard input. It is usually used in combination with other commands through piping. With xargs , you can provide standard input as argument to command-line utilities like mkdir and rm .

What is difference between xargs and pipe?

pipes connect output of one command to input of another. xargs is used to build commands. so if a command needs argument passed instead of input on stdin you use xargs... the default command is echo (vbe's example). it breaks spaces and newlines and i avoid it for this reason when working with files.

Why do we use xargs command in Unix?

The xargs command is used in a UNIX shell to convert input from standard input into arguments to a command. In other words, through the use of xargs the output of a command is used as the input of another command.

What is xargs option?

xargs is a Unix command which can be used to build and execute commands from standard input. Importance : Some commands like grep can accept input as parameters, but some commands accepts arguments, this is place where xargs came into picture. -L max-lines : use at-most max-lines non-blank input lines per command line.


1 Answers

xargs can be used when you need to take the output from one command and use it as an argument to another. In your first example, grep takes the data from standard input, rather than as an argument. So, xargs is not needed.

xargs takes data from standard input and executes a command. By default, the data is appended to the end of the command as an argument. It can be inserted anywhere however, using a placeholder for the input. The traditional placeholder is {}; using that, your example command might then be written as:

find /etc -name "*.txt" | xargs -I {} ls -l {} 

If you have 3 text files in /etc you'll get a full directory listing of each. Of course, you could just have easily written ls -l /etc/*.txt and saved the trouble.

Another example lets you rename those files, and requires the placeholder {} to be used twice.

find /etc -name "*.txt" | xargs -I {} mv {} {}.bak 

These are both bad examples, and will break as soon as you have a filename containing whitespace. You can work around that by telling find to separate filenames with a null character.

find /etc -print0 -name "*.txt" | xargs -I {} -0 mv {} {}.bak 

My personal opinion is that there are almost always alternatives to using xargs (such as the -exec argument to find) and you will be better served by learning those.

like image 160
miken32 Avatar answered Oct 05 '22 15:10

miken32