How to Use cat Command in Linux (with Examples)
If you’re using a Linux computer, operations are vastly different as compared to Windows and macOS. You get both a graphic user interface and a command line interface. While GUI seems to be the easy option to execute operations, CLI does have its own benefits. If you are well-versed in all the importantLinux Terminal commands, you can get things done in no time. One of the most used commands on Linux is thecat command. It comes preinstalled as a part of the coreutils package on all Linux distributions, and the syntax is the same for all distros. That said, we will show how to use the cat command with some practical examples in this article.
Before we look at the examples, let’s understand what is the cat command along with its syntax and options. Then, we will learn how to use the cat command efficiently to view single or multiple files, merge files, sort them, and more.
What is thecatCommand in Linux
The cat command stands forconcatenate,and it is one of the most important commands in every Linux user’s toolbox. It was first made for the UNIX operating system but was later adapted by Linux and macOS. The main purpose of this command is file management, and it enables the user to create new files, view file contents, overwrite files, merge two or more files, etc.
How to Use cat Command: Syntax & Options
Before we can dive into some practical examples, let’s see the syntax for thecatcommand in Linux. The syntax is easy and straightforward. Here’s the syntax, where you need to use an option along with the file names depending on the task you wish to perform.
cat <file_name(s)>
Some of the common options to use with thecatcommand are:OptionsDescription-nShow line numbers for all lines-TShow every tab character in the file-eShow the end of every line in the file-sMerge successive empty lines at the end of the file as one-bShow only non-empty lines
cat Command Examples in Linux Terminal
View a Single File
The most common usage of thecatcommand is to view a single file. You can use the following syntax to view a single file using thecatcommand:
cat <file_name>
View Multiple Files
By adding the name of the files one after the other, seperated by spaces and without any commas, you can also use thecatcommand to view multiple files. Check out the following syntax:
cat <file_1><file_2><file_3>
Display Line Numbers
By default, thecatcommand does not display the line numbers of the file contents it outputs. To show line numbers, use the-nflag with the cat command in Linux:
cat -n <file_name>
Create a New File with cat Command
Generally, we use thetouchcommand to create a new fileor a text editor to create and edit a file. Obviously, thecatcommand cannot replace these tools, but you can use thecatcommand for some quick editing of files. With thecatcommand, you can create a new file and add some content to it. The syntax to create a new file using thecatcommand is:
cat > <new_file_name>
Here, the “>” is known as theoverwrite operatorand is used to overwrite any file with new content. Since the file is completely empty, whatever you write, gets written to the file. When you are done writing to the new file, press “ENTER” and then use “CTRL + d"to exit the prompt.
In the example above, you can see that a new file “test1.txt” is created using the cat command, and the file contents are shown by the output of the secondcatcommand.
Merge Two Files into a New File
Using the syntax below, you can even use thecatcommand to combine two files into one. We will be using the append operator (“»“) to add the contents of the first file at the end of the second file using the command below.
cat <file_1> » <file_2>
In the above example, the contents of the file “test1.txt” are added at the end of the “test2.txt” using thecatcommand. The new contents can be verified with the secondcatcommand’s output, where we view the second file.
Copy the Content of One File to Another
You can even copy the content of a file to another file using thecatcommand, as explained below. Here, the “>” is used to overwrite the contents offile_1tofile_2.
cat <file_1> > <file_2>
In the above example, we have overwritten the contents of the file “test1.txt” with the contents of the file “test2.txt” using the overwrite operator.
Display Invisible Characters
By default, the cat command does not mark the line endings while printing the contents of a file. To show the line endings, use the-Eflag along with the command:
cat -E <file_name>
This willmark the ending of each line with a"$“symbol. To print the tabs instead of four blank spaces, use either the-Tflag, as per the syntax shown below:
cat -T <file_name>
This willprint all tab characters as “^I“. To print all other invisible characters, use the-vflag with the cat command, as shown in the syntax below:
cat -v <file_name>
As you can see in the example above, all the line endings are marked with a “$” symbol, and the tabs are marked with a “^I” character.
Combine Multiple Empty Lines as One
Sometimes there may be some empty lines in the file that you do not want to print. To merge all empty lines as one, use the-sflag with the original cat command.
cat -s <file_name>
View File Contents in Reverse Order (tac Command)
Generally, thecatcommand displays the file content in top-down format. But, while storing some live stream data or viewing some large log file, the latest data gets appended at that end and it can be difficult to scroll through the huge text block. In such cases, you can use thetaccommand in Linux, an alternative version of thecatcommand, whichprints the file contents in reverse order. The syntax to use thetaccommand is:
tac <file_name>
Sorting Output Contents of Files
In Linux, you can combine two or more commands with the help of shell redirectors. They redirect the output of one command to the input of the next command. You can use the overwrite operator (>) and the append operator (»), which are known as I/O shell redirectors.
There is also a second type of shell redirector known as shell piping which is used to run two or more commands concurrently. This means the output of one command will be redirected to the next command as the input. Since the command execution follows a definite construct, such a construct or concept is known as a pipeline. The pipe operator ( | )creates a pipeline for these commands to execute in a definite sequence.
By now, you must be well aware that thecatcommand prints the file contents in the same order as they are stored in the file. As the name suggests, thesortcommand classifies the output in ascending or descending order. But by sending the output of thecatcommand via the pipe operator to thesortcommand, you can get the final output in the desired sorted order. This might sound confusing and complicated, but the example below will clear out everything. The syntax to use the two commands using a pipe operator is:
cat <file_name> | sort
In the above example, instead of printing the contents of the file “test3.txt”, the cat command sends the contens to the sort command which thensorts it according to alphabetical orderand finally prints the sorted output.
View Large Files Using cat Command
Sometimes, even a system with great specifications can stutter in showing the contents of a large file. For such large files, you should use thelesscommand and thecatcommand along with the pipe operator. Since thelesscommand only loads a part of the file at a time, it does not consume a ton of resources. You can scroll up or down to visit the other parts of the file using the arrow keys. The syntax to use thelesscommand with thecatcommand is:
cat <big_file_name> | less
In the above example, when you execute the command as per the above syntax, the file does not get printed on the same terminal prompt, instead, it shows the file contents in a new terminal view as shown in the second picture. Here you can scroll through the text using the arrow keys. To get to the bottom of the text use “GG” and to get to the top of the text, use “gg”. To exit the new terminal view, press “q”.
cat Command Practical Examples
The cat command, along with the tac command, greatly simplifies file management for users comfortable using the Linux Terminal. With options and additional operators, the cat command can be immensely helpful in simplifying your workflow. In this article, we have shared some practical examples of how to use the cat command to create, append, and view files on your Linux system. If you want to learn more about the cat command, visit itsofficial man page. If you face any issues when using this command, let us know in the comments below.
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ