• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Use the Cat Linux Command

Bhagwad Park

Published on: April 3, 2024

Categories: Linux Command Line, VPS Hosting 0

“Cat” is one of the most well-known commands in Linux, and has a history dating back to the 1960s and 70s. The original purpose of the command was to concatenate files and display the results on the standard output, from which functionality the command gets its name – short for “concatenate”. Throughout all the many changes that Linux has experienced since then, cat has remained a simple tool, even with all the under-the-hood changes.

In this tutorial, I’ll show you the various use cases of cat, showcasing its versatility.

Table of Contents
  • The Basic Use of Cat
    • Using Line Numbers for Easier Readability
  • Using Cat to Merge Files
  • Appending to Text Files Using Cat
  • Creating a New File
  • Combining with Grep for Finding Strings
  • Conclusion

The Basic Use of Cat

The most common use of cat, even for novice Linux users, is its ability to quickly display the contents of a file without needing to open it in a separate text editor. Unlike an editor like “vi”, which has a separate editing interface and a complete set of commands to learn, you just run the cat command, and the output is shown directly below in the same bash terminal. For example, here’s how to quickly use the cat command to display the contents of a file in Linux:

cat testbackup.txt

Here’s what it looks like:

Displaying the Contents of a File Using Cat
Displaying the Contents of a File Using Cat

As you can see, it quickly shows the file text without any fuss. Particularly for smaller text files without any scrolling, like the one above, it’s incredibly useful. But if your file has a lot of text, it’ll simply scroll past you without giving you a chance to read what it printed to the terminal. To deal with this, you can simply use the “more” command along with cat, which allows you to use the spacebar to scroll through the content.

Here’s what the command looks like:

cat test.txt | more

And here’s the output:

Cat with More
Cat with More

You can see that the interface stops when the page is full, and you can click the spacebar to display the next round of text. You can use this useful trick to regulate the display of text for any Linux command, not just cat. Any time you have to print a large amount of text on the terminal and want to read it page by page, pipe the result to “more” as shown above and you’re set!

Using Line Numbers for Easier Readability

Sometimes a text file is too large to easily scan through, and you want to refer to a specific piece of code or a command in a script. In such situations, it’s easier to provide the line number, instead of the text in the file. Or you might be trying to debug some code, and you know that the error occurs on a certain line number. Cat can help you add line numbers to the contents of the file that it displays using the following command:

cat -n filename.txt

Here’s the output:

Line Numbers in Cat
Line Numbers in Cat

You can see above in the text file standard “Lorem Ipsum” content, that it shows all the places where a newline character exists. It’s important to note that line numbers do not represent the breaks in the terminal window when the text wraps around to the next line, as this will differ depending on the size of the viewport. Instead, line numbers are standard location references that won’t change from system to system.

So if you have to refer to or find a line number for a coding error, just use “cat” with the “-n” flag.

Using Cat to Merge Files

As I mentioned earlier, the original use of cat was to concatenate files, not just display them. Merging files is as easy as displaying them – simply list the file names side by side like this:

cat testbackup.txt example.txt

The above command will take the two files “testbackup.txt” and “example.txt”, and display them one after the other on the terminal like this:

Concatenate and Print
Concatenate and Print

You can see that cat takes the two files and just shows them consecutively. By itself, this is hardly useful. But the cool feature of cat is that you can redirect the output to any file of your choice. So, for example, the following command will merge the two files into a new third file called “mergedfile.txt”:

cat testbackup.txt example.txt > mergedfile.txt

Cat won’t show you anything when you execute the above command, because the output is no longer directed towards the screen, but into the new file. If the file doesn’t already exist, it will be created.

You can repeat the above procedure for any number of files that you want to concatenate – you’re not limited to just two. This was the basic, original use-case scenario for cat.

Appending to Text Files Using Cat

A cool use for cat is appending text. For example, if I want to append text to a file called “example.txt”, I use the following command:

cat >> example.txt

This will give you a new line, where you can manually type or paste the additional content you want to append to the file. When you’re done, press “Ctrt+d”. Then you can display the results using the cat command as before. Here’s what it looks like:

Appending Using Cat
Appending Using Cat

In the above screenshot, I’ve appended the line “This is additional stuff” to the file “example.txt”. Easy, right?

Creating a New File

You can also use the cat command on Linux to create a new file. Like other commands such as “touch”, merely specifying a file that doesn’t exist causes it to be created. For example, to create a file called “newfile.txt”, simply type the command:

cat > newfile.txt

However, cat does more than simply create a new file. You can also type the content that you want in the lines that follow. When you’re done, press “Ctrl+d” as before, and the file is created with your content.

If you just want to create a new file without any content and don’t want to add anything to it, it’s easier to use the “touch” command instead like this:

touch example.txt

These two methods of creating a new file are better alternatives than opening a dedicated text editor like vi, which opens a new interface, especially if the content you want to write in the new text file is just a few lines, for testing purposes, for example.

Combining with Grep for Finding Strings

Since cat sends its output to the standard output, we can easily combine it with other commands like “grep”. For example, let’s say I want to find the text “Lorem” in the file with boilerplate text. I can do so by chaining the two commands together like this:

cat test.txt | grep "Lorem"

This will display the lines in the file where the word “Lorem” appears, and color the matched string so that it’s easy to see. The pipe (|) is one of the most useful tools that Linux admins use all the time. Here’s a tutorial on how to maximize its effectiveness of the pipe command.

If you want to learn more about grep, here’s an article on how to use it with multiple strings at the same time.

Conclusion

You can see that cat is a versatile command in Linux for displaying, merging, and searching text files. For a program that’s so old, it’s held up very well. Hopefully, this tutorial gave you an easy introduction to one of the most venerable Linux commands!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

All About Bash Parameter Expansion

Parameter expansion in bash is very useful when working with strings like filenames and log entries. It allows you to clean up text.

Bash File Descriptors Explained

Bash file descriptors are pointers to open files that a process uses. These included stdin, stdout and the stderr streams.

Symbolic Links vs Hard Links in Linux

Linux offers various methods for referencing files and directories through other files and directories, without creating copies of the original. If you have color coding enabled in your bash terminal, you might have seen some folders full of files of a different color from regular files. There’s a good chance that these are all symbolic […]

Using the “whois” Command – All you Need to Know

If you want to know about who owns a domain, or when it expires, then you need to use the "whois" command. Here's all you need to know.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA