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

How do these three work? Or what are some websites/URLs?


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

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 
  • xargs uses far fewer resources than 'find -exec' – Felipe Alvarez Sep 1 '10 at 13:44
  • 6
    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

See the following articles:

Linux and Unix find command tutorial with examples
xargs: How To Control and Use Command Line Arguments

  • at least one of those links is dead. Its better to just post the relevant code sample as well. – user5359531 Aug 22 '17 at 15:32
  • @user5359531: The dead link was replaced. And I cannot reproduce such voluminous tutorials. – harrymc Aug 22 '17 at 15:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.