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
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:
cat filename.txt
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
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.
Suppressing repeated empty lines
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
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.
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.
Can I use the cat command to display multiple files at once?
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.