The cat andtac commands display the contents of text files, but there's more to them than meets the eye. Dive a little deeper and learn some productive Linux command line tricks.
These are two simple little commands, often dismissed as being just that---too simple to be of any real use. But once you know the different ways you can use them, you'll see that they are perfectly capable of doing their fair share of the heavy lifting when it comes to working with files.
The cat Command
cat is used toexamine the contents of text files, and to join parts of files together to form a larger file.
At one time---back in the era of the dial-up modem---binary files were often broken into several smaller files to make downloading easier. Instead of downloading one large file, you pulled back each smaller file. If a single file failed to download correctly, you would just retrieve that one file again.
Of course, you then needed a way to reconstitute the collection of smaller files back into the single working binary file. That process was called concatenating. And that's wherecat came in and where it gets its name from.
Broadband and fiber connections have caused that particular need to fade---much like screechy dial-ups sounds---so what's left forcat to do today? Quite a lot actually.
Displaying a Text File
To havecat list the contents of a text file to a terminal window, use the following command.
Make sure the file is a text file. If you try to list the contents of a binary file to the terminal window, the results will be unpredictable. You might end up with a locked terminal session or worse.
cat poem1.txt

The contents of the file poem1.txt are shown in the terminal window.

That's only half of the famous poem. Where's the rest of it? There 's another file here called poem2.txt. We can makecat list the contents of multiple files with one command. All we need to do is list the files in order on the command line.
cat poem1.txt poem2.txt

That looks better; we have the whole poem now.

Using cat With less
The poem is all there, but it shot past the window too fast to read the first few verses. We can pipe the output fromcat intoless and scroll down through the text at our own pace.
cat poem1.txt poem2.txt | less

We can now move backward and forward through the text in one stream, even though it is held in two separate text files.

Numbering the Lines in a File
We can have cat number the lines in the file as it is displayed. To do this, we use the-n (number) option.
cat -n poem1.txt

The lines are numbered as they are displayed in the terminal window.

Don't Number Blank Lines
We managed to have the lines numbered bycat, but the blank lines between the verses are being counted as well. To have the text lines numbered but to ignore the blank lines, use the-b (number-nonblank) option.
cat -b poem1.txt

Now the text lines are numbered, and the blanks lines are skipped.

Don't Show Multiple Blank Lines
If there are sections of consecutive blank lines in a file, we can askcat to ignore all but one blank line. Look at this file.

The next command will causecat to display only one blank line from each bunch of blank lines. The option we need to achieve this is the-s (squeeze-blank) option.
cat -s poem1.txt

This doesn't affect the contents of the file in any way; it just changes the waycat displays the file.

Display Tabs
If you want to know whether whitespace is caused by spaces or tabs, you can find out using the-T (show-tabs) option.
cat -T poem1.txt

The tabs are represented by the characters "^I".

Displaying the Ends of Lines
You can check for trailing whitespace by using the-E (show-ends) option.
cat -E poem1.txt

The ends of lines are represented by the "$" character.

Concatenating Files
It doesn't make sense to have a poem saved in two files, with one half in each. Let's join them together and make a new file with the entire poem in it.
cat poem1.txt poem2.txt > jabberwocky.txt

let's usecat to check our new file:
cat jabberwocky.txt

Our new file contains the contents of the other two files.

Appending Text to an Existing File
That's better, but in actual fact, it's not the entire poem. The last verse is missing. The last verse in Jabberwocky is the same as the first verse.
If we've got the first verse in a file, we can add this to the bottom of the jabberwocky.txt file, and we'll have the complete poem.
In this next command, we have to use >>, not just>. If we use a single> we'll overwrite jabberwocky.txt. We don't want to do that. We want to append text to the bottom of it.
cat first_verse.txt >> jabberwocky.txt

Let's check the contents of the jabberwocky.txt file:
cat jabberwocky.txt

And finally, all the parts of the poem are together.











