How to Use the Linux cat Command With Examples
How to Use the Linux cat Command With Examples
2024, 15:02 How to Use the Linux cat Command With Examples
TUTORIALS menu
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.
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
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.
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.
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.
It is great for comparing files or getting a quick overview of multiple files at once.
The below command is useful for creating text files and writing scripts or configuration files directly from the
command line.
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:
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.
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
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.
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:
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
This command concatenates multiple files into a single file. It functions exactly like the redirection feature
above but with multiple source 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.
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.
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 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
Related tutorials
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.
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
Excellent
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
Cloud Hosting
WordPress Hosting
Email Hosting
CMS Hosting
Ecommerce Hosting
cPanel Hosting
Online Stores
Website Builder
Buy Hosting
DOMAINS
Domain Checker
Domain Transfer
Free Domain
XYZ Domain
99 Cent Domains
WHOIS Checker
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
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.
https://www.hostinger.com/tutorials/linux-cat-command-tutorial-and-examples/ 10/10