Unix command line tricks for Linux, Mac, and Windows: cat

in #technology7 years ago

See Unix command line tricks for Linux, Mac, and Windows: table of contents for an introduction to this series and links to all the entries. Today we look at cat, a command for quickly looking at files. We'll start by trying it out with this file, which I've called testfile1.txt:

Here is the first line of testfile1.txt.
This is the second and final line of testfile1.txt. 

Entering the following at the command line will show you the contents of the file at the command line. I won't even show you the output, because it's identical to what you see above:

cat testfile1.txt

I use cat every time I want to look at the contents of a file that is 25 lines or less, which means I use it dozens of times a day.

Like all the other commands in this series, using the man ("manual") command to learn more about cat by entering man cat told me about some great options for this command that definitely fell into the category of "where have you been all my life." For example, -n ("number") tells cat to number the lines in the output, so that entering this

cat -n testfile1.txt

gives us this:

 1  Here is the first line of testfile1.txt.
 2  This is the second and final line of testfile1.txt.

If you want a copy of the file with the numbers, you can use the redirect symbol to tell cat to store its output in a file instead of outputting it to your terminal:

cat -n testfile1.txt > testfile1a.txt

For some more cool cat command line options, we'll use this sample file, which I called testfile2.txt:

Here is the first line of testfile2.txt.
Here is the second line. I'm leaving the third line blank: 

This is the fifth line. I'm leaving the sixth, seventh, and eighth lines blank: 



This is the ninth and final line of testfile2.txt. 

An -n switch would tell cat to add numbers to all 9 lines of testfile2.txt, but -b ("number-nonblank") tells it to number only the nonblank lines, so that this command

cat -b testfile2.txt

gives us this:

1   Here is the first line of testfile2.txt.
2   Here is the second line. I'm leaving the third line blank: 

3   This is the fifth line. I'm leaving the sixth, seventh, and eighth lines blank: 



4   This is the ninth and final line of testfile2.txt. 

The -s switch ("squeeze-blank") tells cat that when it finds multiple blank lines in a row, it should only output one, so that this command

cat -s testfile2.txt

gives us this output:

Here is the first line of testfile2.txt.
Here is the second line. I'm leaving the third line blank: 

This is the fifth line. I'm leaving the sixth, seventh, and eighth lines blank: 

This is the ninth and final line of testfile2.txt. 

I can think of a lot of data cleanup I've done where that would have been handy if I'd known about it.

Why does cat have that name? Because it's short for "concatenate," which is something else it can do. If you list multiple files as arguments to the command, it will output all of their contents. If you redirect this output, it will combine the contents of the multiple files into the output file that it creates. For example, the following will create testfile3.txt as a copy of testfile1.txt followed by a copy of testfile2.txt:

cat testfile1.txt testfile2.txt > testfile3.txt

Try out man cat yourself to learn more about this very popular command.

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65092.40
ETH 3470.06
USDT 1.00
SBD 2.50