Useless use of cat
Useless use of cat (UUOC) is common Unix jargon for command line constructs that provide only a function of convenience to the user. In computing, the word "abuse", in the sense of "improper or excessive use", is used to disparage the excessive or unnecessary use of a language construct; thus, abuse of cat is sometimes called "cat abuse". Example of a common cat abuse is:
cat filename | command arg1 arg2 argn
This can be rewritten using redirection of stdin instead, in either of the following forms (the first is more traditional):
command arg1 arg2 argn < filename
<filename command arg1 arg2 argn
Beyond other benefits, the input redirection forms allow command to perform random access on the file, whereas the cat examples do not. This is because the redirection form opens the file as the stdin file descriptor which command can fully access, while the cat form simply provides the data as a stream of bytes.
Another common case where cat is unnecessary is where a command defaults to operating on stdin, but will read from a file, if the filename is given as an argument. This is the case for many common commands; the following examples
cat file | grep pattern
cat file | less
can instead be written as
grep pattern file
less file
A common interactive use of cat for a single file is to output the content of a file to standard output. However, if the output is piped or redirected, cat is unnecessary.
A cat written with UUOC might still be preferred for readability reasons, as reading a piped stream left-to-right might be easier to conceptualize. Also, one wrong use of the redirection symbol > instead of < (often adjacent on keyboards) may permanently delete the content of a file, in other words clobbering, and one way to avoid this is to use cat with pipes. Compare:
command < in | command2 > out
<in command | command2 > out
with:
cat in | command | command2 > out