Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I think these are the common ways of passing the result of one command to the next one, as inputs.

Can anyone here explain to me how these three works? or forward me to some websites/URL?

share|improve this question

First things to type:

man find

man xargs

The find command prints results to standard output by default, so the -print option is normally not needed, but -print0 separates the filenames with a 0 (NULL) byte so that names containing spaces or newlines can be interpreted correctly.

The -exec option is something you can use instead of xargs - the find command executes a command for each item it finds.

The xargs command reads space- or newline-separated strings (typically from the find command, but they could come from anywhere) and executes some command for each string.
If xargs is run with a -0 option, it'll expect NULL-separated strings as output by find ... -print0

The advantage of xargs is that it can group the strings together, so that it only executes a command once or twice instead of n times.

So in the normal usage:

find start_directory -name '*.txt' | xargs ls -l 

find would list the filenames, and xargs would issue commands like:

ls -l file1.txt file2.txt file3.txt ... fileN.txt

which is faster than having your find command issuing:

ls -l file1.txt
ls -l file2.txt
ls -l file3.txt
ls -l ...
ls -l fileN.txt
share|improve this answer

Note that xargs is no more needed with current find implementations that probably all support this POSIX syntax:

find directory -name '*.txt' -exec ls -l {} + 

which is simpler and slightly faster than the xargs variant.

find directory -name '*.txt' | xargs ls -l 
share|improve this answer
xargs uses far fewer resources than 'find -exec' – Felipe Alvarez Sep 1 '10 at 13:44
That's the other way around. Piping to xargs will use slightly more resources than -exec. You probably overlook the "+" terminator. – jlliagre Sep 1 '10 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .