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

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
Issue | Cause | Solution |
cat: filename: No such file or directory | File does not exist or path is incorrect | Double-check the filename and path. Use ls to verify existence. |
Permission denied | You don’t have permission to read the file | Use sudo cat filename.txt or change file permissions if appropriate. |
Terminal stuck after cat > file.txt | cat is waiting for input | Type content, then press Ctrl + D to save and exit. |
Overwriting a file unintentionally | Using > instead of >> | Use >> to append instead of overwrite. |
Blank output | File is e-7mpty or all lines are blank | Check 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
.