Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 don't quite understand pipes in Linux command line.

I noticed that:

ll - R | grep *.pdf

will list files ending with .pdf


locate *.pdf | du -h

will not calculate the size of files ending with .pdf. Rather it will list the size of files in the current directory.

What is going wrong here?

What I have in mind is the output of the first command is the input of the next.

share|improve this question
up vote 7 down vote accepted

Pipes work by sending one program's output to another program's input. This means that the program receiving the output of the other has to be able to read from STDIN (standard streams).

In this case, grep is able to read the output of ll because it is designed that way. du expects a command line argument pointing to the directory it should run in (if a directory isn't given, it will default to the current working directory ./).

As for seeing the sizes of the .pdf files, if all the files are in a directory, you can run du -h -d1 /path/to/pdf/dir or locate *.pdf | xargs | du -h. If they are in different directories, you will want to use -exec and find together (another user will probably give you a hand with this, I'm not quite sure how to do it).

share|improve this answer
By the way, can you give me some example commands that are designed to read from STDIN – xiaohan2012 Dec 13 '11 at 6:59
Instead of du -h -d1 /path/to/pdf/dir, it is also possible to use locate *.pdf | xargs | du -h – bbaja42 Dec 13 '11 at 8:46
@bbaja42 thanks, I'll add that to the post. – n0pe Dec 13 '11 at 21:18
@bbaja42 There's one too many | characters there. try locate *.pdf | xargs du -h instead (i.e. no | between xargs and du). Also, if any of the filenames contain spaces or newlines, it's safer to use locate -0 *.pdf | xargs -0 -r du -h (assuming your locate is actually mlocate or some other locate that has the -0 option). Finally, this won't work correctly if there any .pdf files in the current directory because the shell will expand the glob before passing it to locate - use locate -0 .pdf instead. – cas Mar 15 at 10:42

What I have in mind is the output of the first command is the input of the next.

That is correct. However, in your two examples, the difference is that grep acts on its input whereas du does not. Piping works as expected only if the first command gives something to the standard output and the second takes something from the standard input, in which case thes two flows will be connected through a "pipe". You can find out if this is possible by looking for "standard input" and "standard output" in the man pages of the commands.

share|improve this answer
I do not quite understand why grep acts on its input whereas du does not. How can judge that grep can while du can not. Or given another program, how can I determine whether it can or not act on its input? – xiaohan2012 Dec 13 '11 at 6:57
Both man grep and grep --help tell you in the first lines that standard input is accepted by this command. This should be the case for any command accepting STDIN. – Dalker Dec 13 '11 at 7:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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