In the beginning there was cat …

… and I’m not talking about Ceiling Cat. I’m talking about the Linux command cat.

Apropos the idea that one should not use cat to produce a stream of text for a command that takes a filename as a argument … I say “Balderdash!”

If your plan is to process the text from a file with a single command and that’s that .. no modifications will be needed … then by all means, you will save time and computing resources (though immeasurably little of either) by using the filename as an argument to said command.

But if you are not quite sure how you are going to get to your eventual final results, you might find that staring with cat will give you a paws up when you need to start fiddling. The cat command will even take arguments that can help you.

In fact, I think it is the widespread conception that cat is ‘merely a filter’ that does nothing but pass the contents of the file it is fed contributes to the belief that cat is always useless. In fact, cat is a very powerful command.

Cat counts as a filter. A file goes in and comes out. When cat is issued with nothing other than a filename as an argument, it does nothing but stream out the contents of the file. This is convenient if you want to construct a complex command that starts with the contents of a file running into standard input. For instance:

cat mouse.txt

will simply ‘print’ to the terminal the contents of moue.txt. But that’s OK. You can do this and verify that mouse.txt exists and contains roughly what you thought it contained. Then you can add something to this such as

cat mouse.txt | grep mice

which gets me:

Three blind mice.

Three blind mice.

As three blind mice.

I could have gone

grep mice mouse.text

and gotten the same thing, but that would interfere with the poetry of

cat mouse.txt | grep mice | wc

which gets me: 2 10 66

which is, of course, either Daryl Johnston’s or Rodney Anoa’i’s birthday.

Here’s another good one. A common question given to programmers looking for work, as part of their application, is this:

“Write a perl one liner that adds numbers to the lines in a file.”


Who needs perl? … cat -n mouse.txt

which gets me this:

1 Three Blinde Mice,
2 three Blinde Mice,
3 Dame Iulian,
4 Dame Iulian,
5 The Miller and his merry olde Wife,
6 shee scrapte her tripe licke thou the knife.
8 Three blind mice. Three blind mice.
9 See how they run. See how they run.
10 They all ran after the farmer’s wife
11 She cut off their tails with a carving knife.
12 Did you ever see such a thing in your life
13 As three blind mice.

or, cat -b mouse.txt

which is similar but only numbers non-blank lines.

or the -s option, one of the coolest, which does not allow more than one blank line at a time through the filter!

or -T which shows tabs, otherwise invisible.

Man cat. Try it, you’ll like it.


  1. #1 Quiet Desperation
    August 19, 2009

    The cat command works great on my Mac. BSD Unix underneath, don’t ya know. 🙂

  2. #2 mk
    August 19, 2009

    Maybe it’s just the difficulty reading the output on this page in the exact same font as the discussion, but I don’t understand how “cat mouse.txt|grep mice|wc” returns “2 10 66” when “cat -n mouse.txt” shows four lines containing the word “mice” (1, 2, 8, 13).

  3. #3 mk
    August 19, 2009

    Never mind, I missed that lines 1 and 2 have “Mice” not “mice.” And/or I was thinking grep -i.

  4. #4 Spiv
    August 20, 2009

    “type” is the win/dos equiv. which is stupid. But similar in principle. Pipe it to find and you basically have the same function as grep. Type doesn’t have a numbering function, but find does and will give you the same output:

    type mouse.txt | find /n /v “”

    and…uh…I need perl. Because it’s awesome.

    Admittedly “type /?” does not have nearly the impact of “man cat”.

  5. #5 LightningRose
    August 23, 2009

    You neglected to mention one of the commands primary purposes, which is to concatenate files. For example,

    cat file1 file2 file3 >bigfile

    copies file1 to bigfile, then appends file2 to bigfile, and finally appends file3 to bigfile.