How to Use cat Command in Linux (with Examples)

The cat command is one of the most frequently used commands in Linux. It is used to display the contents of a file or to create a new file. In this article, we will explain how to use the cat command in Linux with some examples.

1. Display the Contents of a File

The most common use of the cat command is to display the contents of a file. To do this, you just need to type the following command:

$ cat filename

This will display the contents of the file named “filename” on the screen.

2. Create a New File

You can also use the cat command to create a new file. To do this, you need to use the “>” operator. For example, to create a new file named “newfile”, you can type the following command:

$ cat > newfile

This will create an empty file named “newfile”. You can then type the contents of the file and press Ctrl+D to save the file.

3. Concatenate Multiple Files

You can also use the cat command to concatenate multiple files. To do this, you need to specify the names of the files that you want to concatenate. For example, to concatenate the files “file1”, “file2” and “file3”, you can type the following command:

$ cat file1 file2 file3

This will display the contents of all the files one after the other.

4. Append Data to a File

You can also use the cat command to append data to an existing file. To do this, you need to use the “>>” operator. For example, to append the contents of the file “file1” to the file “file2”, you can type the following command:

$ cat file1 >> file2

This will append the contents of the file “file1” to the end of the file “file2”.

These are some of the most common uses of the cat command in Linux. You can use this command to display the contents of a file, create a new file, concatenate multiple files and append data to an existing file.

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.

cat Command in Linux: Explained (2023)

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:

Options Description
-n Show line numbers for all lines
-T Show every tab character in the file
-e Show the end of every line in the file
-s Merge successive empty lines at the end of the file as one
-b Show 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

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.

How to Use cat Command in Linux (with Examples)

The cat command is one of the most frequently used commands in Linux. It is a very useful tool for viewing, creating, and concatenating files. In this article, we will explain how to use the cat command in Linux with some examples.

What is the cat Command?

The cat command (short for “concatenate”) is one of the most versatile commands in Linux. It can be used to view the contents of a file, create new files, and concatenate files. It is also used to display the contents of a file on the terminal.

Syntax of the cat Command

The syntax of the cat command is as follows:

cat [options] [file_name]

Examples of the cat Command

Let’s look at some examples of how to use the cat command in Linux.

1. View the Contents of a File

To view the contents of a file, use the following command:

cat filename

This will display the contents of the file on the terminal.

2. Create a New File

To create a new file, use the following command:

cat > filename

This will create a new file with the specified name. You can then enter the contents of the file and press Ctrl+D to save the file.

3. Concatenate Files

To concatenate two or more files, use the following command:

cat file1 file2 file3 > newfile

This will create a new file called “newfile” which contains the contents of all the specified files.

Conclusion

In this article, we have explained how to use the cat command in Linux with some examples. The cat command is a very useful tool for viewing, creating, and concatenating files. We hope you have found this article helpful.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?