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 important Linux Terminal commands, you can get things done in no time. One of the most used commands on Linux is the cat 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 the cat Command in Linux

The cat command stands for concatenate, 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 the cat command 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 <options> <file_name(s)>

Some of the common options to use with the cat command 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 the cat command is to view a single file. You can use the following syntax to view a single file using the cat command:

cat <option> <file_name>

view single file

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 the cat command to view multiple files. Check out the following syntax:

cat <option> <file_1> <file_2> <file_3>

view multiple files

Display Line Numbers

By default, the cat command does not display the line numbers of the file contents it outputs. To show line numbers, use the -n flag with the cat command in Linux:

cat -n <file_name>

display line numbers using cat command

Create a New File with cat Command

Generally, we use the touch command to create a new file or a text editor to create and edit a file. Obviously, the cat command cannot replace these tools, but you can use the cat command for some quick editing of files. With the cat command, you can create a new file and add some content to it. The syntax to create a new file using the cat command is:

cat > <new_file_name>

Here, the “>” is known as the overwrite operator and 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.

create new file using the cat command

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 second cat command.

Merge Two Files into a New File

Using the syntax below, you can even use the cat command 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 <option> <file_1> >> <file_2>

Merge two files using the cat command

In the above example, the contents of the file “test1.txt” are added at the end of the “test2.txt” using the cat command. The new contents can be verified with the second cat command’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 the cat command, as explained below. Here, the “>” is used to overwrite the contents of file_1 to file_2.

cat <file_1> > <file_2>

overwrite a file with another

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 -E flag along with the command:

cat -E <file_name>

This will mark the ending of each line with a "$" symbol. To print the tabs instead of four blank spaces, use either the -T flag, as per the syntax shown below:

cat -T <file_name>

This will print all tab characters as “^I. To print all other invisible characters, use the -v flag with the cat command, as shown in the syntax below:

cat -v <file_name>

display invisible characters using the cat command

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 -s flag with the original cat command.

cat -s <file_name>

merging empty lines as one using the cat command

View File Contents in Reverse Order (tac Command)

Generally, the cat command 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 the tac command in Linux, an alternative version of the cat command, which prints the file contents in reverse order. The syntax to use the tac command is:

tac <file_name>

view contents of a file in reverse order

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 the cat command prints the file contents in the same order as they are stored in the file. As the name suggests, the sort command classifies the output in ascending or descending order. But by sending the output of the cat command via the pipe operator to the sort command, 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 <options> <file_name> | sort

sorting file contents on alphabetical order using the cat command and the sort command

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 then sorts it according to alphabetical order and 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 the less command and the cat command along with the pipe operator. Since the less command 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 the less command with the cat command is:

cat <big_file_name> | less

  • How to Use cat Command in Linux (with Examples)
  • How to Use cat Command in Linux (with Examples)

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 its official man page. If you face any issues when using this command, let us know in the comments below.

#Tags
Comments 0
Leave a Reply

Loading comments...