Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the UNIX command xargs

Tags:

unix

xargs

I'm pretty much confused on this. Need some clarifications.

Example 1 :

pgrep string | xargs ps 

Example 2 :

find . | xargs grep whatever 

From Example 1, I gather it's this way:

Search for a "string" which is part of name of running process and return the process-ids of all the matches to 'xargs ps' -> which just appends ps to the matches (which are process-ids themselves) to get the same output as :

ps <processid> 

Can someone explain what xargs really does in this case?

From Example 2, I gather it's this way:

It's to search for some "string" recursively from the current working directory. Here, how exactly does 'xargs' work?

I was of the opinion that 'xargs' repeatedly appends data from standard input to the 'argument' given to xargs (which usually is a UNIX command by itself).

From xargs() man page :

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

like image 587
halluc1nati0n Avatar asked Dec 13 '09 22:12

halluc1nati0n


People also ask

How does xargs command work?

The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.

What is xargs option?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.


2 Answers

In general xargs is used like this

prog | xargs utility

where prog is expected to output one or more newline/space separated results. The trick is that xargs does not necessarily call utility once for each result, instead it splits the results into sublists and calls utility for every sublist. If you want to force xargs to call utility for every single result you will need to invoke it with xargs -L1.

Note that xargs promises you that the sublist sent to utility is shorter than ARG_MAX (If you're curious, you can get the current value of ARG_MAX using getconf ARG_MAX.) This is how it avoids those dreaded "Argument list to long" errors.

like image 58
Lars Tackmann Avatar answered Oct 13 '22 21:10

Lars Tackmann


A good example of what xargs does is to try getting sorted checksums for every file in a directory using find.

find . | cksum  | sort 

returns just one checksum, and it's not clear what it's the checksum for. Not what we want. The pipe sends the stdout from find into stdin for cksum. What cksum really wants is a list of command line args, e.g.

cksum file001.blah file002.blah  file003.blah 

will report three lines, one per file, with the desired checksums. Xargs does the magic trick - converting stdout of the previous program to a temporary and hidden command line to feed to the next. The command line that works is:

find . | xargs cksum | sort 

Note no pipe between xargs and cksum.

like image 22
DarenW Avatar answered Oct 13 '22 21:10

DarenW