06.12.
2024, 15:02 How to Use the Linux cat Command With Examples
TUTORIALS menu
VPS Jul 30, 2024 Anish 5min Read
How to use the Linux cat command
The cat command is one of the most useful commands in Linux – it is used for displaying, combining, and
manipulating text files.
Its versatility is what makes it an indispensable tool for everyday Linux administration and scripting. For
instance, it can be used to quickly view the contents of configuration files inside the /etc directory or,
concatenate multiple files into one.
What’s more, the cat command comes pre-installed with the coreutils package in any Debian and Red Hat-
based system, so you don’t need to install it separately.
In this tutorial, we will go through the essential and common uses of the Linux cat command and explain its
features with examples.
Syntax of the cat command
Common use cases and examples of the Linux cat command
Displaying file contents
Displaying multiple files
Creating new files
Appending to files
Displaying line numbers
Suppressing repeated empty lines
Showing tabs and end of lines
Redirecting to a new file
Combining and redirecting multiple files
Linux cat command FAQ
What does the cat command do in Linux?
Can I use the cat command to display multiple files at once?
Can I use the cat command to display invisible characters like tabs and line
endings?
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 1/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
Syntax of the cat command
TUTORIALS menu
Before we start to deep-dive into the article’s subject, you should log into the VPS using SSH, to run the
below cat commands and check its basic syntax.
The basic syntax of the cat command is:
cat [OPTION] [FILENAME]
The command takes the name of one or more files you want to look at or change as an argument, along
with the additional settings that change how the command works.
Here’s a breakdown of each part of the command:
cat: This is the command itself. It stands for “concatenate” and is used to read and display the
contents of files.
[OPTION]: These are optional flags that modify the behavior of the cat command. For example, -n
adds line numbers to the output, and -s squeezes multiple adjacent blank lines into a single blank line.
Later in the tutorial, we will demonstrate how to use these options to perform particular operations.
[FILENAME]: This is the name of one or more files that you want to view or manipulate. You can list
multiple filenames separated by spaces to concatenate their contents.
To find all available options for the cat command, just type cat –help in your terminal or use the cat
command manual using the command below:
man cat
This is what the manual’s page for the cat command looks like:
This manual page provides in-depth information about the cat command, including its name, synopsis, and
description. It also outlines the different options that can be utilized with the cat command. This page is a
helpful resource for understanding the various available options and the operations they perform.
Common use cases and examples of the Linux cat
command
The Linux cat command can be used in various situations to perform essential tasks. Here are a few
examples.
Displaying file contents
It is one of the most basic and common uses of the cat command. If you want to see what’s inside a file,
simply use the command:
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 2/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
cat filename.txt
TUTORIALS menu
cat
This command will display all the contents of filename.txt right in your terminal. It’s perfect for quickly
checking small files without opening a text editor.
One of the most practical uses of this command is to view the contents of configuration files inside the /etc
directory and quickly check the content of log files inside the /var/log directory.
We can use the following command to display the content of multiple files of the same format:
cat *.txt
To prevent scrolling large files add the option | more to the end of the command. It reads the file content
page by page, making it easier to navigate through large text files.
cat filename.txt | more
Displaying multiple files
Instead of displaying the contents of a single file in the console, you can use the following command to
display the content of multiple files:
cat file1.txt file2.txt file3.txt
It is great for comparing files or getting a quick overview of multiple files at once.
Creating new files
We can use the cat command to create new files and easily add data to them. This can be particularly
useful for quickly generating and filling text files without needing to open a separate text editor.
The below command is useful for creating text files and writing scripts or configuration files directly from the
command line.
cat > newfile.txt
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 3/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
TUTORIALS menu
After running this command, a new file is created, and you can begin populating it with text. To add multiple
lines of text, just press Enter at the end of each line. Once you’re done, hit CTRL+D then CTRL+C to save
and exit the file.
Voila! You’ve created a new file. If the destination file does not exist, this command will create it or overwrite
the existing one with the same name.
Appending to files
To add more data to an existing file, we can use the cat command with the append >> operator:
cat >> filename.txt
Simply enter some text and press Ctrl+D to save.
To verify if the newly added data has been appended to the file, let’s use the cat command:
cat filename.txt
This command is useful for appending text and configurations to existing files or adding entries to log files.
Displaying line numbers
Sometimes, it’s helpful to see line numbers alongside the content. To use this feature, use the -n option with
the cat command:
cat -n filename.txt
This technique is especially useful for debugging scripts by referencing line numbers or reviewing code with
line references.
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 4/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
Suppressing repeated empty lines
TUTORIALS menu
To suppress repeated blank lines in the output, you can use the -s option along with the Linux cat command.
Do note that this option will keep one blank line by removing the repeated blank lines only. The command
would look like this:
cat -s filename.txt
This is useful for cleaning up file output and formatting documents and files for better readability.
In the above output, you can see that the file filename.txt contains six lines, and the last three lines are
empty. The -s option combines multiple adjacent empty lines into a single empty line, making the output
cleaner and more readable.
Showing tabs and end of lines
When you need to see invisible characters, such as tabs and the end of lines, you can use the options -T and
-E options together. The cat command marks line ends by displaying the $ character at the end of each line,
and it shows tab characters as ^I.
cat -T -E filename.txt
Using both options together will display the contents of filename.txt with visible indicators for tabs and end
of lines. This can be especially useful for:
Identifying and correcting tab and space inconsistencies.
Ensuring that file formatting is consistent, especially in code or configuration files where such details
matter.
Debugging issues where invisible characters may be causing problems in scripts or data processing.
Redirecting to a new file
You can redirect the output of one file to another file using the > redirect operator. The command looks like
this:
cat file1.txt > file4.txt
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 5/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
This command is useful for creating backups of files and redirecting output to a new file. If the destination
file does not exist, this command will create it or overwrite an existing one with the same name.
TUTORIALS menu
Combining and redirecting multiple files
To combine multiple files and redirect the output to a new file, you can use the below command:
cat file1.txt file2.txt file3.txt > combinedfile.txt
This command concatenates multiple files into a single file. It functions exactly like the redirection feature
above but with multiple source files.
It is useful when merging log files and combining configuration files.
To append the contents of the source file to an existing file, you can use the append >> operator along with
the cat command to avoid overwriting it.
cat file2.txt file3.txt >> file1.txt
Conclusion
The Linux cat command might seem simple at first, but as you’ve seen, it’s a versatile command line tool.
From viewing and creating files to combining and manipulating them, the cat command is often the fastest
and most reliable alternative.
Remember, the best way to become comfortable with the cat command is to use it regularly.
Practice using the cat command with different options to explore its full potential and enhance your
command-line skills. As a next step, learn how to master other basic Linux commands.
Linux cat command FAQ
What does the cat command do in Linux?
The cat command in Linux is primarily used to display file contents, concatenate files, and create new
files. It can also append text to existing files, number lines, and reveal non-printing characters.
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 6/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
Can I use the cat command to display multiple files at once?
TUTORIALS menu
Yes, just list the filenames as arguments, separated by spaces. For example, cat file1.txt file2.txt
file3.txt will display the content of all three files in sequence.
Can I use the cat command to display invisible characters like tabs and
line endings?
Yes, use the -T option for tabs and the -E option for line endings: cat -T -E filename.txt. Combining
these will display all non-printing characters, which is great for troubleshooting.
THE AUTHOR
Anish Singh Walia
With over 5+ years of experience as a Cloud Consultant and SRE(DevOps) and 7+ years of
experience as a freelance technical writer Anish loves to write about anything technical as
Related tutorials
29 Nov • VPS • LARGE SCALE ECOMMERCE
Best Magento 2 extensions to enhance your store’s functionality
Magento 2 is an open-source eCommerce platform that helps businesses and developers create online stores. In addition to its
core features, users can...
By Savictor Sobechi Evans-Ibe
29 Nov • VPS • GAMING
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 7/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
Minecraft server commands: most popular commands and how to use them
Running a Minecraft TUTORIALS menu
server can be challenging – admins spend time dealing with griefers, managing complex player permissions,
and wrestling with...
By Valentinas C.
29 Nov • VPS • GAMING
How to add plugins to a Minecraft server: Using Game Panel and FTP
Whether you’re looking to add basic commands, implement an economy system, or create custom game modes, plugins provide
the foundation for...
By Ritoban Mukherjee
What our customers say
Excellent
Based on 36,167 reviews
Leave a reply
Comment*
Name*
Email*
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 8/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
By using this form you agree that your personal data would be processed in accordance with our Privacy Policy.
TUTORIALS menu
Submit
We are a web hosting provider on a mission to bring success to everyone who goes online. We do it by constantly improving server technology,
providing professional support, and making the web hosting experience seamless.
And More
HOSTING
Web Hosting
VPS Hosting
Minecraft Server Hosting
CyberPanel VPS Hosting
Cloud Hosting
WordPress Hosting
Email Hosting
CMS Hosting
Ecommerce Hosting
cPanel Hosting
Online Stores
Website Builder
Buy Hosting
Web Hosting for Agencies
Cheap Web Hosting
DOMAINS
Domain Checker
Domain Transfer
Free Domain
XYZ Domain
99 Cent Domains
Cheap SSL Certificate
Buy Domain Name
WHOIS Checker
Free SSL Certificate
Domain Name Search
HELP
Tutorials
Knowledge Base
Report Abuse
INFORMATION
Server Status
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 9/10
06.12.2024, 15:02 How to Use the Linux cat Command With Examples
Affiliate Program
Payment Methods TUTORIALS menu
Wall of Fame
Reviews
Pricing
COMPANY
About Hostinger
Our Technology
Career
Contact Us
Blog
LEGAL
Privacy Policy
Terms of Service
© 2004-2024 hostinger.com - Premium Web Hosting, Cloud, VPS & Domain Registration Services.
Prices are listed without VAT
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 10/10