• Follow us


How to Use the Linux cat and tac Commands

Art of a terminal window on a Linux desktopFatmawati Achmad Zaenuri/Shutterstock.com

The cat and tac 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 to examine 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 where cat 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 for cat to do today? Quite a lot actually.

Displaying a Text File

To have cat 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

cat poem1.txt in a terminal window

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

the contents of poem1.txt in a 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 make cat 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

cat poem1.txt poem2.txt in a terminal window

That looks better; we have the whole poem now.

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

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 from cat into less and scroll down through the text at our own pace.

cat poem1.txt poem2.txt | less

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

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

content of poem1.txt and poem2.txt in less in a terminal window

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

cat -n poem1.txt in a terminal window

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

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

Don’t Number Blank Lines

We managed to have the lines numbered by cat, 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

cat -b poem1.txt in a terminal window

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

numbered lines skipping blank l;ines in a terminal window

Don’t Show Multiple Blank Lines

If there are sections of consecutive blank lines in a file, we can ask cat to ignore all but one blank line. Look at this file.

contents of a file with consecutive blank lines in a terminal window

The next command will cause cat 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

cat -b poem1.txt in a terminal window

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

Multiple blank lines being ignored in a terminal window

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

cat -T poem1.txt in a terminal widow

The tabs are represented by the characters “^I”.

contents ofpoem1.txt with tabs displayed in a terminal window

Displaying the Ends of Lines

You can check for trailing whitespace by using the -E (show-ends) option.

cat -E poem1.txt

cat -T poem1.txt in a terminal window

The ends of lines are represented by the “$” character.

content of poem1.txt with line ends displayed in a terminal window

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

cat poem1.txt poem2.txt > jabberwocky.txt in a terminal window

let’s use cat to check our new file:

cat jabberwocky.txt

cat jabberwocky.txt in a terminal window

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

contents of jabberwocky.tx in a terminal window

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

cat first_verse.txt >> jabberwocky.txt in a terminal window

Let’s check the contents of the jabberwocky.txt file:

cat jabberwocky.txt

cat jabberwocky.txt in a terminal window

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

contents of jabberwocky.txt in a terminal window

Redirecting stdin

You can redirect input from the keyboard into a file using cat. Everything you type is redirected into the file until you hit Ctrl+D. Note that we use a single > because we want to create the file (or overwrite it, if it exists).

cat > my_poem.txt

cat > my_poem.txt in a terminal window

We can start typing as soon as we issue the command. We hit Ctrl+D when we’ve finished. We can then check the contents of the new file with:

cat my-poem.txt

Low grade doggerel in a terminal window

That sound like a far-off turbine is probably Lewis Carroll spinning in his grave at high speed.

The tac Command

tac is similar to cat, but it lists the contents of files in reverse order.

Let’s see that:

tac my_poem.txt

tac my_poem.txt in a terminal window

And the file is listed to the terminal window in reverse order. In this case, it has no effect on its literary merits.

my_poem.txt listed in reverse order in a terminal window

Using tac With stdin

Using tac without a filename will cause it to operate on the input from the keyboard. Hitting Ctrl+D will stop the input phase, and tac will list in reverse order whatever you’d typed in.


tac working with stdin in a terminal window

When Ctrl+D is hit, the input is reversed and listed to the terminal window.

output from tac using stdin in a terminal window

Using tac With Log Files

Apart from low-grade parlor tricks, can tac do anything useful? Yes, it can. Many log files append their newest entries at the bottom of the file. Using tac (and, counterintuitively, head) we can pop the last entry into the terminal window.

We use tac to list the syslog file in reverse, and pipe it into head. By telling head to only print the first line it receives (which thanks to tac is the last line in the file), we see the latest entry in the syslog file.

tac /var/log/syslog | head -1

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

head prints the latest entry from the syslog file and then exits.

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

last entry from syslog in a terminal window

Using tac with Text Records

The last trick tac has up its sleeve is a beauty.

Usually, tac operates on text files by working its way through them line by line, from the bottom up. A line is a sequence of characters terminated by a newline character. But we can tell tac to work with other delimiters. This allows us to treat “chunks” of data within the text file as data records.

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

less logfile.dat

less logfile in a terminal window

As we can see, there is a repeating format to the file. There are sequences of three lines of hexadecimal values. Each set of three lines of hexadecimal has a label line that starts “=SEQ”, followed by a sequence of digits.

top of the logfile in a terminal window

If we scroll to the bottom of the file, we can see that there are a lot of these records. The final one is numbered 865.

bottom of the logfile in a terminal window

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

We’ll make a note that the final three lines in the file start with hexadecimal values 93, E7 and B8, in that order.

Let’s use tac to reverse the file. It is a very long file so we’ll pipe it into less.

tac logfile.dat | less

tac logfile.dat | less in a terminal window

That reverses the file, but it isn’t the result we want. We want the file to be reversed, but the lines in each data record must be in their original order.

reversed logfile in less in a terminal window

We recorded earlier that the final three lines in the file start with hexadecimal values 93, E7 and B8, in that order. The order of those lines has been reversed. Also, the “=SEQ” lines are now below each set of three hexadecimal lines.

tac to the rescue.

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

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

Let’s break that down.

The -s (separator) option informs tac what we want to use as the delimiter between our records. It tells tac not to use its usual newline character, but to use our separator instead.

The -r (regex) option tells tac to treat the separator string as a regular expression.

The -b (before) option causes tac to list the separator before each record instead of after it (which is the usual position of its default separator, the newline character).

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

The ^ character represents the start of the line. This is followed by =SEQ.+[0-9]+*$. This instructs tac to look for each occurrence of “=SEQ.” at the start of a line, followed by any sequence of digits (indicated by [0-9]), and followed by any other set of characters (indicated by *$).

We’re piping the whole lot into less, as usual.

reversed logfile with correctly formed data records

Our file is now presented in reverse order with each “=SEQ” label line listed before its three lines of hexadecimal data. The three lines of hexadecimal values are in their original order within each data record.

We can check this simply. The first value of the first three lines of hexadecimal (which were the last three lines before the file was reversed) match the values that we took a record of earlier: 93, E7 and B8, in that order.

That’s quite a trick for a terminal window one-liner.

Everything Has a Purpose

In the Linux world, even the seemingly simplest commands and utilities can have surprising and powerful properties.

The design philosophy of simple utilities that do one thing well, and which easily interwork with other utilities, has given rise to some strange little commands, such as tac. At first glance, it appears to be a bit of an oddity. But when you peer beneath the surface, there is an unexpected power that you can leverage to your advantage.

Or, as another philosophy says, “Do not despise the snake for having no horns, for who is to say it shall not become a dragon?”

Read More

Leave A Comment

More News


Foxconn president resigns to run for office 2019-06-21 08:00:29He wants to focus on his presidential campaign.

Google confirms it's leaving the tablet business 2019-06-21 07:58:09It's throwing everything it has into the laptop business.

US city votes to pay ransomware demand 2019-06-21 07:30:31Riviera Beach can't catch a break.

iPaaS: The true digital transformation enabler 2019-06-21 07:00:33At the heart of any digital transformation project is the same principle – getting access to data and managing that data effectively.

5G can help start ups compete better 2019-06-21 06:30:585G could give birth to a whole new wave of start-up businesses, who would leverage the technology to compete better against well-established players i

Leaked passwords are only the tip of the 2019-06-21 06:30:45The true cause of the problem isn’t what one company does or doesn’t do with their security, but the underlying premise that personally id

The rise of voice commerce 2019-06-21 06:00:46This is a burgeoning trend that could be a huge market in the very near future.

IT issues creating workplace "black hole" 2019-06-21 06:00:33Employees are losing hours fixing stuff around the office.

GDPR compliance: is your business at risk of 2019-06-21 05:30:57Since the introduction of GDPR last year, small businesses have faced increased pressure to develop and alter their existing policies in line with the

How continuous deployment can help you keep pace 2019-06-21 05:00:10With every company now a software company, here's how continuous deployment makes you stand out from the crowd.

Keeping up with digital transformation: Is your ERP 2019-06-21 04:30:46Digital transformation need not be a scary term, but the foundation of your ERP strategy.

Why the jewellery sector is in major need 2019-06-21 04:00:07How blockchain and modern technology has helped to change the way the sector is functioning.

TechRadar: Internet news

Jamaica vs USA live stream: how to watch 2019-07-03 18:41:42Reggae Boyz out to make their third consecutive Gold Cup final as they face the USMNT. Check out our Jamaica vs USA live stream guide for all your wat

Symantec reportedly in Broadcom takeover talks 2019-07-03 17:04:33Broadcom may be looking at Symantec deal following last year's CA Technologies acquisition

Chile vs Peru live stream: how to watch 2019-07-03 16:56:12Who will win the 'Pacific derby' and join Brazil in the 2019 Copa América final? Check out our guide for your Chile vs Peru live stream.

Parallels and Winzip developer Corel acquired 2019-07-03 16:47:45Investment equity firm KKR snaps up Corel less than one year after Parallels deak.

The best VR laptops: these notebooks are ready 2019-07-03 16:24:29Don't have room for a VR gaming PC in the house? Then you're going to need a powerful laptop.

Copa América 2019 live stream: how to watch 2019-07-03 15:51:00The last four in Brazil and a classic encounter beckons. Read our guide for your 2019 Copa América live stream options as we enter the semi-fin

The best gaming monitor 2019: the 10 best 2019-07-03 15:49:44With the best gaming monitors, your games will come alive in ways you can’t even imagine.

The best Chromebooks 2019 2019-07-03 15:33:50We've searched far and wide for the best Chromebooks you can buy, always up to date and thoroughly tested.

The best PC gaming headsets 2019 2019-07-03 15:20:26The best gaming headset brings your game audio to life and won't cramp your ears after long play sessions. Here are the top 15 headsets we've tested

Netherlands vs Sweden live stream: how to watch 2019-07-03 14:58:58It's all or nothing in the second 2019 FIFA Women's World Cup semi-final. Don't miss a kick with our Netherlands vs Sweden live stream guide.

Best gaming laptops 2019: the 10 top gaming 2019-07-03 14:40:15The best gaming laptops you can buy, always up to date with the latest hardware configurations.

Popular cloud storage app hides a rather nasty 2019-07-03 14:35:43Upstream has blocked over 114m suspicious mobile transactions.

Enterprise – TechCrunch

Equinix and Singapore’s GIC will launch a $1 2019-07-02 00:20:35Equinix, one of the world’s largest data center companies, announced that it will form a $1 billion joint venture with GIC, Singapore’s so

Video platform Kaltura adds advanced analytics 2019-07-01 15:15:26You may not be familiar with Kaltura‘s name, but chances are you’ve used the company’s video platform at some point or another, give

We’ll talk even more Kubernetes at TC Sessions: 2019-07-01 12:00:58You can’t go to an enterprise conference these days without talking containers — and specifically the Kubernetes container management syst

Tara.ai, which uses machine learning to spec out 2019-07-01 06:09:59Artificial intelligence has become an increasingly important component of how a lot of technology works; now it’s also being applied to how tech

Enterprise SaaS revenue hits $100B run rate, led 2019-06-28 11:48:44In its most recent report, Synergy Research, a company that monitors cloud marketshare, found that enterprise SaaS revenue passed the $100 billion run

We’re talking Kubernetes at TC Sessions: Enterprise with 2019-06-27 12:48:01Over the past five years, Kubernetes has grown from a project inside of Google to an open source powerhouse with an ecosystem of products and services

Fellow raises $6.5M to help make managers better 2019-06-27 11:21:30Managing people is perhaps the most challenging thing most people will have to learn in the course of their professional lives – especially beca

Fungible raises $200 million led by SoftBank Vision 2019-06-27 11:00:24Fungible, a startup that wants to help data centers cope with the increasingly massive amounts of data produced by new technologies, has raised a $200

Cathay Innovation leads Laiye’s $35M round to bet 2019-06-27 10:22:46For many years, the boom and bust of China’s tech landscape have centered around consumer-facing products. As this space gets filled by Baidu, A

Amperity update gives customers more control over Customer 2019-06-27 09:03:26The Customer Data Platform (CDP) has certainly been getting a lot of attention in marketing software circles over the last year as big dawgs like Sale

Bright Machines wants to put AI-driven automation in 2019-06-26 11:16:00There’s a mythology around today’s factories that says everything is automated by robotics, and while there is some truth to that, it&rsqu

Vulcan Cyber announces $10M Series A to automate 2019-06-26 09:20:42Many software vulnerabilities are already known, and vendors have even issued patches, but the problem is there are so many patches that it’s of

Disclaimer and Notice:WorldProNews.com is not responsible of these news or any information published on this website.