Fatmawati Ahmad Zaenuri / Shutterstock.com

Teams cat and tac display the contents of text files, but there is more to it than meets the eye. Dive a little deeper and learn some useful Linux command line tricks.

These are two simple little commands, often ignored as too simple to actually use. But once you know how they can be used in different ways, you will realize that they are quite capable of doing their hard work when it comes to working with files.

cat team

cat used to check the contents of text files and merge parts of files into one big file.

Back in the dial-up era, binaries were often split into several smaller files to make downloading easier. Instead of uploading one large file, you returned each smaller file. If any file failed to download correctly, you will simply get that file again.

Of course, then you needed a way to restore a collection of small files back into a single working binary. This process has been called unification. And that’s where it came from cat and where he got his name from.

Broadband and fiber optic connections have made this special need disappear — just like the sounds of screeching dialing — so what’s left to do cat today? Quite a lot actually.

Text file display

To cat list the contents of a text file into a terminal window, use the following command.

Make sure the file is a text file. If you try to display the contents of a binary file in a terminal window, the results will be unpredictable. You could end up with a locked terminal session or worse.

  кот poem1.txt 

cat poem1.txt in terminal window

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

the contents of poem1.txt in the terminal window

This is only half of the famous poem. Where is the rest? There is another file here called poem2.txt. We can do it cat list the contents of multiple files with a single command. All we have to do is list the files in order on the command line.

  кот poem1.txt poem2.txt 

cat poem1.txt poem2.txt in terminal window

It looks better; we have a whole poem now.

contents of poem1.txt and poem2.txt in terminal window

Use of a cat at a lower cost

The whole poem is there, but it flew past the window too quickly to read the first few verses. We can redirect output from cat in less and scroll down the text at your own pace.

  кошка poem1.txt poem2.txt |  Меньше 

cat poem1.txt poem2.txt |  less in terminal window

Now we can move forward and backward through the text in one stream, even if it is contained in two separate text files.

the content of poem1.txt and poem2.txt is smaller in the terminal window

Line numbering in a file

We can specify the line number in the file as it is displayed. For this we use the option -n (number).

  cat -n poem1.txt 

cat -n poem1.txt in terminal window

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

the contents of poem1.txt with numbered lines in the terminal window

Do not number empty lines

We managed to number the lines with cat but empty lines between verses are also counted. To have text line numbering but ignore empty lines, use the option -b (number-nonblank).

  cat -b poem1.txt 

cat -b poem1.txt in terminal window

Now text lines are numbered and blank lines are skipped.

numbered lines are skipped;  l;  ines in a terminal window

Don’t show multiple blank lines

If the file has sections of consecutive empty lines, we can ask cat ignore all but one empty line. Look at this file.

file contents with consecutive blank lines in a terminal window

The following command will force cat display only one empty line from each group of empty lines. The option we need for this is the option -s (squeeze-blank).

  cat -s poem1.txt 

cat -b poem1.txt in terminal window

This does not affect the contents of the file in any way; it only changes the way cat displays the file.

Multiple blank lines ignored in terminal window

Tab display

If you want to know if spaces are caused by spaces or tabs, you can find out using the option -T (show-tabs).

  cat -T poem1.txt 

cat -T poem1.txt in terminal widow

Tabs are represented by the symbols «^I».

contents ofpoem1.txt with tabs displayed in terminal window

Line end display

You can check for spaces using the option -E (show-end).

  кот -E poem1.txt 

cat -T poem1.txt in terminal window

Line ends are represented by the «$» symbol.

the content of poem1.txt with line endings is displayed in the terminal window

Combining files

It does not make sense to save the poem in two files, one half in each. Let’s merge them together and create a new file with the entire poem.

  cat poem1.txt poem2.txt> jabberwocky.txt 

cat poem1.txt poem2.txt> jabberwocky.txt in terminal window» width=»646″ height=»57″ src=»https://gadgetshelp.com/wp-content/uploads/images/htg/content/uploads/2019/06/cat_19.png»></p>
<p><noscript><img class=

Our new file contains the contents of two other files.

contents of jabberwocky.tx in terminal window

Adding text to an existing file

It’s better, but it’s not really the whole poem. The last verse is missing. The last verse in Jabberwocky is the same as the first verse.

If we have the first verse in the file, we can add it to the end of the jabberwocky.txt file and we have the complete verse.

In this next command we have to use >> not just > . If we use one > we overwrite jabberwocky.txt. We don’t want to do this. We want add text to the end.

  cat first_verse.txt >> jabberwocky.txt 

cat first_verse.txt >> jabberwocky.txt in terminal window» width=»646″ height=»57″ src=»https://gadgetshelp.com/wp-content/uploads/images/htg/content/uploads/2019/06/cat_22.png»></p>
<p><noscript><img class=

And finally, all parts of the poem together.

contents of jabberwocky.txt in terminal window

Redirecting standard input

You can redirect keyboard input to a file using cat . Whatever you type is redirected to a file until you press Ctrl+D. Please note that we are using single > because we want to create the file (or overwrite it if it exists).

  cat> my_poem.txt 

cat> my_poem.txt in terminal window» width=»646″ height=»57″ src=»https://gadgetshelp.com/wp-content/uploads/images/htg/content/uploads/2019/06/xcat_24.png.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ ri+cp+md.ic.otdNynEbgw.png»></p>
<p><noscript><img class=

This sound is like a distant turbine, probably Lewis Carroll spinning at high speed in the grave.

Tac Command

tac look like cat but lists the contents of the files in reverse order.

Let’s see what:

  tac my_poem.txt 

tac my_poem.txt in terminal window

And the file is displayed in the terminal window in reverse order. In this case, it does not affect its literary merit.

my_poem.txt is listed in reverse order in a terminal window

Using TAC with STDIN

Usage tac without a filename will cause it to work with keyboard input. Pressing Ctrl + D will stop the input phase and tac will reverse everything you typed.


TAC works with STDIN in a terminal window

Pressing Ctrl + D reverses the input and displays it in the terminal window.

output from TAC using standard input in a terminal window

Using TAC with Log Files

Apart from low-quality tricks, can anything be done useful? Yes, it can. Many log files add their newest entries at the bottom of the file. Using tac (and, illogically, head ), we can pop the last entry into the terminal window.

We use tac to output the log file in reverse order and head him in head . talking head print only the first line received (thanks to tac is the last line in the file), we see the last entry in the log file.

  tac / var / log / syslog |  голова -1 

tac /var/log/syslog |  head -1 in terminal window

head prints the last entry from the system log file and then exits.

note that head prints only one line — as we asked — but the line is so long that it wraps around twice. That’s why it looks like three lines of output in the terminal window.

last entry from the system log in the terminal window

Using TAC with text entries

The last trick that has sleeves is beauty.

Usually tac works with text files, passing through them line by line, from bottom to top. A string is a sequence of characters ending with a newline character. But we can say tac to work with other delimiters. This allows us to treat «chunks» of data in a text file as data records.

Let’s say we have a log file of some program that we need to view or analyze. Let’s look at its format with less .

  меньше logfile.dat 

less log file in terminal window

As we can see, there is a repeating format in the file. There are sequences of three strings of hexadecimal values. Each set of three hexadecimal strings has a label string that starts with «=SEQ» followed by a sequence of digits.

the top of the log file in the terminal window

If we scroll to the end of the file, we will see that there are many of these entries. Final number 865.

the bottom of the log file in the terminal window

Let’s assume that for some reason we need to work through this file in reverse order, data entry by data entry. The line order of the three hexadecimal strings in each data entry must be preserved.

Note that the last three lines in the file start with the hexadecimal values ​​93, E7, and B8, in that order.

Let’s use tac to flip the file. This is a very long file, so we will add less .

  tac logfile.dat |  Меньше 

tac logfile.dat |  less in terminal window

This reverses the file, but this is not the result we want. We want the file to be reversed, but the lines in each data entry must be in their original order.

reverse log file smaller in terminal window

We noted earlier that the last three lines in the file start with the hexadecimal values ​​93, E7, and B8, in that order. The order of these lines was reversed. Also, the «=SEQ» lines are now below each set of three hexadecimal strings.

tac for help.

  tac -b -r -s ^ = SEQ. + [0-9] + * $ logfile.dat |  Меньше 

tac -b -r -s ^ = SEQ.  + [0-9] + * $ logfile.dat |  less in terminal window

Let’s deal with this.

Option -s (separator) tells tac what we want to use as a separator between our entries. He says tac don’t use your regular newline character, but use our delimiter instead.

Option -r (regex) specifies tac treat the delimiter string as a regular expression.

Option -b (before) forces tac enumerate delimiter before each entry, not after it (which is the normal position of its default separator, the newline character).

Line -s (separator) ^=SEQ.+[0-9]+*$ is decrypted as follows:

Symbol ^ denotes the beginning of a line. Followed by =SEQ.+[0-9]+*$ . It instructs tac look for every occurrence of «=SEQ» at the beginning of a string followed by any sequence of digits (denoted [0-9] ) and then any other set of characters (denoted *$ ).

As usual, we do everything less .

reverse log file with well-formed data records

Our file is now presented in reverse order, where each line of the «=SEQ» label is listed before three lines of hexadecimal data. The three strings of hexadecimal values ​​are in their initial order in each data record.

We can easily check this. The first value of the first three hex lines (which were the last three lines before the file was accessed) matches the values ​​we took earlier: 93, E7, and B8, in that order.

This is just a trick for one-line terminal windows.

Everything has a purpose

In the Linux world, even the simplest-looking commands and utilities can have amazing and powerful features.

The philosophy of designing simple utilities that do one thing well and interact easily with other utilities has given rise to some weird little commands like tac . At first glance, this is a little strange. But when you peer below the surface, there is an unexpected power that you can use to your advantage.

Or, as another philosophy says: «Do not despise the snake because it has no horns, for who can say that it will not become a dragon?»

Похожие записи