How to Use cat Command in Linux (with Examples).
If you’re using a Linux laptop, operations are vastly completely different as in comparison with Windows and macOS. You get each a graphic person interface and a command line interface. While GUI appears to be the simple choice to execute operations, CLI does have its personal advantages. If you might be well-versed in all of the necessary Linux Terminal instructions, you may get issues accomplished very quickly. One of essentially the most used instructions on Linux is the cat command. It comes preinstalled as part of the coreutils package deal on all Linux distributions, and the syntax is similar for all distros. That stated, we’ll present how to make use of the cat command with some sensible examples on this article.
cat Command in Linux: Explained (2023)
Before we take a look at the examples, let’s perceive what’s the cat command together with its syntax and choices. Then, we’ll learn to use the cat command effectively to view single or a number of information, merge information, kind them, and more.
What is the cat
Command in Linux
The cat command stands for concatenate, and it is among the most necessary instructions in each Linux person’s toolbox. It was first made for the UNIX working system however was later tailored by Linux and macOS. The predominant objective of this command is file administration, and it permits the person to create new information, view file contents, overwrite information, merge two or more information, and so on.
How to Use cat Command: Syntax & Options
Before we are able to dive into some sensible examples, let’s see the syntax for the cat
command in Linux. The syntax is straightforward and easy. Here’s the syntax, the place you could use an choice together with the file names relying on the duty you want to carry out.
cat <choices> <file_name(s)>
Some of the frequent choices to make use of with the cat
command are:
Options | Description |
---|---|
-n |
Show line numbers for all strains |
-T |
Show each tab character within the file |
-e |
Show the top of each line within the file |
-s |
Merge successive empty strains on the finish of the file as one |
-b |
Show solely non-empty strains |
cat Command Examples in Linux Terminal
View a Single File
The commonest utilization of the cat
command is to view a single file. You can use the next syntax to view a single file using the cat
command:
cat <choice> <file_name>

View Multiple Files
By including the title of the information one after the opposite, seperated by areas and with none commas, you can even use the cat
command to view a number of information. Check out the next syntax:
cat <choice> <file_1>
<file_2>
<file_3>

Display Line Numbers
By default, the cat
command doesn’t show the road numbers of the file contents it outputs. To present line numbers, use the -n
flag with the cat command in Linux:
cat -n <file_name>

Create a New File with cat Command
Generally, we use the contact
command to create a brand new file or a textual content editor to create and edit a file. Obviously, the cat
command can’t exchange these instruments, however you need to use the cat
command for some fast modifying of information. With the cat
command, you’ll be able to create a brand new file and add some content material to it. The syntax to create a brand new file using the cat
command is:
cat > <new_file_name>
Here, the “>
” is called the overwrite operator and is used to overwrite any file with new content material. Since the file is totally empty, no matter you write, will get written to the file. When you might be accomplished writing to the brand new file, press “ENTER” after which use “CTRL + d" to exit the immediate.

In the instance above, you’ll be able to see {that a} new file “test1.txt” is created using the cat command, and the file contents are proven by the output of the second cat
command.
Merge Two Files right into a New File
Using the syntax under, you’ll be able to even use the cat
command to mix two information into one. We shall be using the append operator (“>>
“) so as to add the contents of the primary file on the finish of the second file using the command under.
cat <choice> <file_1> >> <file_2>

In the above instance, the contents of the file “test1.txt” are added on the finish of the “test2.txt” using the cat
command. The new contents might be verified with the second cat
command’s output, the place we view the second file.
Copy the Content of One File to Another
You may even copy the content material of a file to a different file using the cat
command, as defined under. Here, the “>” is used to overwrite the contents of file_1
to file_2
.
cat <file_1> > <file_2>

In the above instance, we’ve 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 doesn’t mark the road endings whereas printing the contents of a file. To present the road endings, use the -E
flag together with the command:
cat -E <file_name>
This will mark the ending of every line with a "$"
image. To print the tabs as an alternative of 4 clean areas, use both the -T
flag, as per the syntax proven under:
cat -T <file_name>
This will print all tab characters as “^I
“. To print all different invisible characters, use the -v
flag with the cat command, as proven within the syntax under:
cat -v <file_name>

As you’ll be able to see within the instance above, all the road endings are marked with a “$” image, and the tabs are marked with a “^I” character.
Combine Multiple Empty Lines as One
Sometimes there could also be some empty strains within the file that you don’t want to print. To merge all empty strains as one, use the -s
flag with the unique cat command.
cat -s <file_name>

View File Contents in Reverse Order (tac Command)
Generally, the cat
command shows the file content material in top-down format. But, whereas storing some dwell stream knowledge or viewing some massive log file, the newest knowledge will get appended at that finish and it may be troublesome to scroll by the massive textual content block. In such circumstances, you need to use the tac
command in Linux, another model of the cat
command, which prints the file contents in reverse order. The syntax to make use of the tac
command is:
tac <file_name>

Sorting Output Contents of Files
In Linux, you’ll be able to mix two or more instructions with the assistance of shell redirectors. They redirect the output of 1 command to the enter of the subsequent command. You can use the overwrite operator (>) and the append operator (>>), that are often called I/O shell redirectors.
There can also be a second sort of shell redirector often called shell piping which is used to run two or more instructions concurrently. This means the output of 1 command shall be redirected to the subsequent command because the enter. Since the command execution follows a particular assemble, such a assemble or idea is called a pipeline. The pipe operator ( | ) creates a pipeline for these instructions to execute in a particular sequence.
By now, you have to be effectively conscious that the cat
command prints the file contents in the identical order as they’re saved within the file. As the title suggests, the kind
command classifies the output in ascending or descending order. But by sending the output of the cat
command by way of the pipe operator to the kind
command, you may get the ultimate output within the desired sorted order. This would possibly sound complicated and sophisticated, however the instance under will filter out every little thing. The syntax to make use of the 2 instructions using a pipe operator is:
cat <choices> <file_name> | kind

In the above instance, as an alternative of printing the contents of the file “test3.txt”, the cat command sends the contens to the kind command which then kinds it in line with alphabetical order and at last prints the sorted output.
View Large Files Using cat Command
Sometimes, even a system with nice specs can stutter in displaying the contents of a big file. For such massive information, you need to use the much less
command and the cat
command together with the pipe operator. Since the much less
command solely hundreds part of the file at a time, it doesn’t eat a ton of assets. You can scroll up or down to go to the opposite elements of the file using the arrow keys. The syntax to make use of the much less
command with the cat
command is:
cat <big_file_name> | much less
In the above instance, while you execute the command as per the above syntax, the file doesn’t get printed on the identical terminal immediate, as an alternative, it reveals the file contents in a brand new terminal view as proven within the second image. Here you’ll be able to scroll by the textual content using the arrow keys. To resolve the textual content use “GG” and to get to the highest of the textual content, use “gg”. To exit the brand new terminal view, press “q”.
cat Command Practical Examples
The cat command, together with the tac command, significantly simplifies file administration for customers snug using the Linux Terminal. With choices and extra operators, the cat command might be immensely useful in simplifying your workflow. In this text, we’ve shared some sensible examples of how to make use of the cat command to create, append, and examine information in your Linux system. If you need to be taught more in regards to the cat command, go to its official man web page. If you face any points when using this command, tell us within the feedback under.
Check out more article on – How-To tutorial and latest highlights on – Technical News