ISO27001:2013
Certified Supplier

FIND OUT MORE

We're an ISO27001:2013 Certified Supplier

blog-post-featured-image

cat is a tool to read data from standard input or a file and emit it on standard output. That sounds simple – and it is – but it has some very useful applications; not least, reading a file straight to the current terminal for inspection without any other tools.

But this simplicity and utility also leads to many uses of cat which aren’t necessary and in fact add some (minor) processing overhead. Some Unix fans are so upset by “useless uses of cat” that they have founded an award in its honour.

Let’s take a look at some common and useless uses of cat, and how they can be replaced by shell built-ins instead.

cat | grep: search within file

Reading a file for standard input into another tool is very common. This example could be used as follows:

In this example, cat reads the file myfile.txt and passes its contents to grep for searching. However, grep can read the file itself and that saves an additional process and a pipeline:

cat | mysql: send a file to standard input

A common operation is to read a file of SQL instructions (such as a backup) from disk, and pass it to the mysql command for execution:

POSIX-compatible shells, including bash, can read the file onto standard input and save an additional process and a pipeline:

If you have a longer pipeline and it makes sense to think about data flow from left to right, it’s even possible to place the standard input at the beginning of the pipeline:

cat | sed: retrieve a set range of lines from a file

Fetching a pre-defined set of lines from a file is easy with sed:

You can probably see where this is going; sed can read the file itself:

What’s different here though is that mygiantfile.txt is very large, and so to send it all into sed’s standard input only to discard most of it is very inefficient. Allowing sed to read the file itself means it can do efficient seeking on the file and not read it all into memory first!

Legitimate use of cat

There are times when cat is very useful for making basic changes to the input stream, as the manual suggests:

It’s also sometimes necessary to read multiple input files sequentially and provide them all in one standard input, for example when constructing TLS certificate chains:

 

Photo by Lorenzo Herrera on Unsplash

 

Leave a Reply

Your email address will not be published. Required fields are marked *