Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

How to Use the Linux cat and tac Commands

img_5c94224b1cfd7
4
By Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he became a full-time technology journalist. His first published article, describing macros in 6502 assembly language, was published in the December 1985 edition of the UK magazine Personal Computer World. His work has appeared on How-To Geek since March 2019. Despite graduating from Sheffield University as an archaeologist, during his career he's worked as a freelance programmer, manager of an international software development team, project manager for IT installations, and a Data Protection Officer. Dave is a Linux evangelist and open source advocate. Since 1997, all of Dave's computers have run Linux. He is a frequent guest speaker at national conferences, usually talking about topics such as artificial intelligence, data protection legislation, and cybersecurity. Expect detailed how-to's, distribution reviews, and Linux-centric editorials. 
Sign in to yourHow-To Geek account
Jump links

Jump Links

follow
Follow
followed
Followed
Here is a fact-based summary of the story contents:
Try something different:

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

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 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

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 fromcat intoless 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 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

cat -b poem1.txt in a terminal window

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

cat poem1.txt in a terminal window

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.

cat poem1.txt in a terminal window

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

cat poem1.txt in a terminal window

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

cat poem1.txt 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 poem1.txt in a terminal window

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

cat poem1.txt 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 poem1.txt in a terminal window

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

cat poem1.txt 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 in a terminal window

let's usecat to check our new file:

cat jabberwocky.txt

the contents of poem1.txt in a terminal window

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

the contents of poem1.txt 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

the contents of poem1.txt in a terminal window

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

cat jabberwocky.txt

the contents of poem1.txt in a terminal window

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

the contents of poem1.txt in a terminal window
Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
A relaxed man lounging on an orange beanbag watches as a friendly yellow robot works on a laptop for him, while multiple red exclamation-mark warning icons float around them.
3 reasons why vibe coding is a terrible idea
A MacBook surrounded by a gear symbol, a shield, an iCloud icon, and a password dots bar.
I made my Mac more secure by changing these 5 settings
A Chromebook keyboard with the search button as the center focus.
These 5 Chromebook tips save me tons of time in Google Docs
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp