It's been nearly 20 years since I first came across the Useless Use of Cat (UUOC) awards. Unix notable and Perl disciple Randall Schwartz had begun handing out these embarrassing awards around 1995 to people who used commands such as
cat myfile | head -3 when they could more easily have used
head -3 myfile. Wasting system cycles and spawning unneeded processes is not a big sin when it comes to Unix, but it's clearly both wasteful and, well, indicative of users who are either falling into a bad habit or just not paying attention.
The worse offense, using pipes when they simply don't work, is generally a sign that someone is still trying to understand how the Unix command line works. The fact is that some Unix commands are engineered to read command output that is piped to them while others are not. Recognizing the difference takes time and a bit of patience, but pipe mastery is well worth the investment.
[ Prove your expertise with the free OS in InfoWorld's Linux admin IQ test round 1 and round 2. | Track the latest trends in open source with InfoWorld's Open Sources blog and Technology: Open Source newsletter. ]
For the UUOC transgression, the basic thing to keep in mind is that many commands can work directly with files as easily as they can work with content that is piped to them. You can sort a file with
sort myfile as easily as you can do the same thing with
cat myfile | sort. And if minimizing your typing is a sign of Unix wisdom, well then, the first command is going to keep your fingers from tapping any more keys than necessary so bring on the smarts. In fact, there may be very few justifiable uses for commands that begin with
cat myfile |. One example is when you want to do something like this -- though I can't say I've ever seen anyone actually doing this:
$ cat myfile | tee file1 file2 file3
In this command, we get a chance to view the contents of a file and redirect its content to several other files in one command. A more useful example of the "cat file |" command prefix might be this:
$ cat myfile | mailx -s "data file 12" firstname.lastname@example.org">email@example.com
Here we're sending the content of a file to someone's inbox. This kind of command proves useful in many situations, especially when collecting and emailing system performance or status information to yourself or a group of admins.