Linux cat Command: A Complete Guide to Viewing and Combining Files

Linux Cat Command

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

The Linux cat command is short for concatenate, which is one of the most used and versatile commands in Linux. Whether you are a beginner or an advanced user looking to combine multiple files, view the contents of a file, or redirect the output, cat is a power tool. 

At its core, cat lets you:

  • Display file contents
  • Combine files into one
  • Create or append to files directly from the terminal

The simplicity and the flexibility of the Linux cat command makes it a go-to for quick file inspections, scripting tasks, and file manipulation. 

In this guide, we will walk through the ways in which to use the cat command effectively. 

Why Use cat in Linux?

The cat command is a lightweight and fast way to handle text files directly from the Linux command-line tool. It is mainly useful when you want to: 

  • Display the file contents without having to open a text editor
  • Combine or merge files easily 
  • Create a new text file from the output 
  • Redirecting the output for scripting and automation 

Whether you are managing configuration files or debugging the scripts, the Linux cat command offers a simple yet powerful way to interact with file data. 

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Basic Syntax of cat

cat [options] [file1] [file2] …

  • file1, file2, etc. are the files you want to read or concatenate.
  • Commonly used options include:
    • -n: Number all output lines
    • -b: Number non-blank lines only
    • -s: Squeeze multiple empty lines into one

Example:

cat file.txt

How to View File Contents Using cat

To view the contents of a text file, use: 

cat filename.txt

This will display the entire file contents to the terminal. 

Example:

cat /etc/os-release

This will display the Linux distribution information. 

Displaying Multiple Files Together

You can list multiple files at once with the Linux cat command to display them in a sequence, using: 

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

cat file1.txt file2.txt

This command will display the content of both files in a sequence. 

You can also redirect the output to a new file:

cat file1.txt file2.txt > combined.txt

This will merge the contents of both files into combined.txt.

Creating Files with the cat Command

The Linux cat command can help create new files directly from the terminal app by combining it with the output redirection (>): 

cat > filename.txt

After running the command, type out your content and save. Texit, press Ctrl + D.

Example:

cat > hello.txt

Hello, world!

Ctrl + D

This will create a new file called hello.txt containing the text Hello, world!.

Redirecting Output to a New File

If you want to copy the content of one text file to another, you can use: 

cat original.txt > copy.txt

This will create a new copy.txt containing the same content as original.txt.

Appending Content to an Existing File

To add the contents of one text file to end of another file, use the append operator sign >>:

cat source.txt >> destination.txt

This adds the contents of source.txt to the end of destination.txt without deleting existing data.

You can also manually append new lines:

cat >> notes.txt

New line of text here.

Ctrl + D

This appends the line to notes.txt without erasing what’s already there.

Showing Line Numbers with cat -n

The -n flag with the Linux cat command is helpful when you want to view a file content along with the code line numbers. This is especially useful when you need to debug the scripts or review the logs. 

cat -n filename.txt

This does not modify the original file — it only displays numbered lines in the output.

Related Article: Mastering the Linux tail Command: View Logs & Output Like a Pro

Using cat with Pipes and Other Commands

The Linux cat command is used in combination with pipes (|) to send the output to other commands for processing. 

Examples:

  • Count lines in a file: cat filename.txt | wc -l
  • Search for a keyword: cat log.txt | grep “ERROR”
  • Display file content page by page: cat longfile.txt | less

While the Linux cat command is not always necessary in such cases, it can help visualize the code flow in a clear manner. 

cat vs tac, more, and less

Understanding when you need to use the cat command and when it is the right time for alternatives can help improve the workflow. 

  • cat: Displays file contents in full, from top to bottom.
  • tac: Displays file contents in reverse (bottom to top) by running tac filename.txt
  • more: Shows file contents one screen at a time; press Space to scroll.
  • less: More flexible than more; supports scrolling up/down and searching, for example, less filename.txt

Use the Linux cat command when you need to view and quickly merge small files. For bigger files, go with less or more. 

Common Errors and How to Fix Them 

IssueCauseSolution
cat: filename: No such file or directoryFile does not exist or path is incorrectDouble-check the filename and path. Use ls to verify existence.
Permission deniedYou don’t have permission to read the fileUse sudo cat filename.txt or change file permissions if appropriate.
Terminal stuck after cat > file.txtcat is waiting for inputType content, then press Ctrl + D to save and exit.
Overwriting a file unintentionallyUsing > instead of >>Use >> to append instead of overwrite.
Blank outputFile is e-7mpty or all lines are blankCheck file size with ls -l or view hidden characters with cat -A.

Conclusion: Mastering the cat Command in Linux

The Linux cat command is one of the most useful and fundamental tools in the Linux-command line toolkit. It shines best when used for small and quick viewing tasks and by mastering the command and the flags, you can streamline daily tasks easily! 

FAQs

How do I use cat to create a new file?

Use cat > filename to create a file. Type your content and press Ctrl + D to save and exit.

How do I show line numbers with cat?

Use the -n flag: cat -n filename.

Why am I getting a “Permission denied” error with cat?

You may not have the required read permissions. Use sudo if necessary or check file permissions with ls -l.

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!