Frequently Asked Question
What is the difference between cat file | grep foo and grep foo file?
Functionally the two pipelines produce the same lines, but cat file | grep foo
forks an extra process (cat) and creates a pipe between it and grep. grep foo file skips both: grep opens the file itself, reads it directly, and
prints matches. The result is identical but with one fewer process and one
fewer kernel buffer in play. The pattern is famous enough to have a name;
"useless use of cat" or UUOC, and people have been gently roasting each other
about it on Usenet since the 1990s.
That said, there are times when the cat form is genuinely useful. It reads
left-to-right, which is sometimes clearer when you're building a pipeline
experimentally. cat *.log | grep foo glues multiple files into one stream
cleanly. And tools that only read from stdin and don't take filenames need
something to pipe into them. The general principle is: prefer the file argument
when the next command accepts one, but don't lose sleep over a stray cat.