Learn a few techniques for avoiding the pipe and making your command-line commands more efficient.
Anyone who uses the command line would acknowledge how powerful the pipe is. Because of the pipe, you can take the output from one command and feed it to another command as input. What's more, you can chain one command after another until you have exactly the output you want.
Pipes are powerful, but people also tend to overuse them. Although it's not necessarily wrong to do so, and it may not even be less efficient, it does make your commands more complicated. More important though, it also wastes keystrokes! Here I highlight a few examples where pipes are commonly used but aren't necessary.
Stop Putting Your Cat in Your Pipe
One of the most common overuses of the pipe is in conjunction with
cat command concatenates multiple files from input into a
single output, but it has become the overworked workhorse for piped
commands. You often will find people using
cat just to output the
contents of a single file so they can feed it into a pipe. Here's
the most common example:
cat file | grep "foo"
Far too often, if people want to find out whether a file contains a
particular pattern, they'll
cat the file piped into a
This works, but
grep can take a filename as an argument directly,
so you can replace the above command with:
grep "foo" file
The next most common overuse of
cat is when you want to sort the
output from one or more files:
cat file1 file2 | sort | uniq
sort supports multiple files as arguments, so you
can replace the above with:
sort file1 file2 | uniq
In general, every time you find yourself catting a file into a pipe,
re-examine the piped command and see whether it can accept files directly
as input first either as direct arguments or as STDIN redirection.
For instance, both
grep can accept files as arguments as
you saw earlier, but if they couldn't, you could achieve the same
thing with redirection:
sort < file1 file2 | uniq grep "foo" < file
Remove Files without xargs
xargs command is very powerful on the command line—in particular,
when piped to from the
find command. Often you'll use the
command to pick out files that have a certain criteria. Once you
have identified those files, you naturally want to pipe that output
to some command to operate on them. What you'll eventually discover
is that commands often have upper limits on the number of arguments
they can accept.