While working or reading about Linux, you may have encountered a snippet of the cat command and wondered what it is. Well, the cat command is an abbreviation of concatenate. If you are working with text, learn everything you need because it can display the contents of single or multiple files without opening them. Life on Linux without the cat command may be difficult because of the fuss of opening the files to view their contents repeatedly until we see the required text.
This guide will explain everything about the cat command in Linux; we’ve tested it on Debian, Fedora, and Linux so that it will work on all the distributions of Linux. After going through this, the readers will learn the following.
- What is a cat Command?
- How to Use a cat Command (with examples)?
- Bonus Tip: Alternative of the cat Command in Linux
Let’s explain them in detail in the next section.
What is a Cat Command?
In Linux, the cat command is there to help users to create, concatenate or view single or multiple files. It is useful when reading all the text files in a directory because who has the time to open or use commands to view the contents manually? Using the cat command, it is simplified. The syntax of the cat command is as follows:
Syntax
cat [options] filename(s)
In the above syntax, the options offer extended functionality to the usability of the cat command. To get the list of options and their functionality, you can use the following command:
$ cat --help
All the supported options, and their usage is described in the above screenshot.
How to Use the Cat Command in Linux?
Using the cat command in Linux is easy as the syntax is simple, as seen below. Let’s have a look at the practical usage of the cat command in Linux:
Example 1: Viewing File’s Content
Using the cat command, users can view the content of single as well as multiple files. For instance, the command provided below will show the content of the file named “testfile” on the terminal:
$ cat testfile
To view multiple files, add a space and file name after the first file, like this:
$ cat testfile testfile2
You can add as many files as possible, but we’d recommend dividing them into pairs to view them more accurately.
Example 2: Creating a File
Creating a text file requires the users to follow this syntax:
$ cat >[File-Name]
As an example, a file named “testfile” is created using the command:
$ cat >testfile
After the command is executed the cursor will start blinking, waiting for the user to enter the text and close it; a key combination of “ctrl+d” is required.
Example 3: Replacing the Content of One File With Another
To replace a file’s contents from another, the following syntax of the cat command is used.
$cat [Source-File] > [Destination-File]
In our case, the source file is “file1” and the destination is “file2”. The content of the “file2” will be replaced with “file1”:
$cat file1 > file2
In the above image, we’ve replaced the contents of “test2” with “test4” Users can also replace the contents of multiple files into one as in this format:
$ cat [Source-File1] [Source-File2] > [Destination-File]
Example 4: Viewing the Contents of a File With Line Number
The cat command comes with an option that allows the user to display the contents of a file along with a line number which could be helpful for code reviewers. The format is as below:
$ cat -n [File-Name]
We used the above syntax on the file named “file1.txt” as follows:
$ cat -n file1.txt
Example 5: Adding the Text to an Already Existing File
Using the cat command, users can also add text to a file that already has text. It will not overwrite the text as in this format:
$ cat >> [File-Name]
Let’s see the practical implementation of the command on “file1.txt”:
$ cat >> file1.txt
As you can see in the above image, we’ve added a new text to a file and then pressed “CTRL + D” to save and exit the file. Lastly, the text of the file is also displayed on the terminal.
Example 6: Viewing the Content of All Files in a Directory
The wild card character “*” is used to select all the files. For instance, the command provided below will display all the content of the “text” files in PWD:
$ cat *.txt
In this command, the content of all files with the “.txt” extension is displayed, and users can view the content of any supported text file.
Example 7: Display $ at the End of Each Line
Using the cat command, users can highlight the end of the line(s) from a text file, making the reading more effective; it can be done using “-E” in the command:
$ cat -E <File-Name>
For practical implementation, the cat command with “-E” option is as follows:
$ cat -E file1.txt
Example 8: Suppress the Empty/Blank Lines
Using the cat command, users can suppress the REPEATED empty lines, which can help them go through the text more efficiently; it can be done by using the “-s” option like this:
$ cat -s <File-Name>
In the above image, we had a file with three blank lines and using the “-s” option, we suppressed the two repeated blank lines.
Example 9: Display the Content of Large Files
While working with large files, the user could be annoyed because going through all that text could be frustrating. So we have the “More and Less” options that can be used to simplify the extensive data. The “Less” option can be used like this:
$ cat <File-Name> | less
The less option will display the contents of the file as a whole, and in the end, you’d see a “:” which indicates the end of the file. By pressing the “ctrl+z” buttons, you can go back to the terminal.
The more command will display the contents of the file, but up to a certain point where your terminal’s screen ends, and after that, users must press the “Enter” key to view the rest of the content. It is used like this.
$ cat <File-Name> | more
This is all about the cat command in Linux.
Bonus Tip: Alternative of the cat Command in Linux
There is another command with the name of a “bat” that you can use as a replacement, giving you the same result but more sophisticated. It doesn’t come pre-installed, so you need to install it:
For Debian-based Distributions:
$ sudo apt install bat
For Arch Based:
$ sudo pacman -S bat
For Fedora:
$ sudo dnf install bat
For CentOS/RHEL:
$ sudo dnf install cargo && cargo install --locked bat
Then you need to run the following command to read the content of the file (in our case, the file is file1.txt):
$ batcat file1.txt
You can see in the above image that it made different sections so that information can be seen in a better way with numbering as well. To get more information about the bat command, you can also take its help by typing:
$ batcat -h
That’s a wrap-up!
Conclusion
The cat command is helpful for users involved with text, including reading and manipulating. This article sheds light on all aspects with the help of examples to explain the use of the cat command that you can use to view data more efficiently. Apart from that, an alternative to the cat command is also described, which can be used in place of the cat.