15 Examples of Using the cat Command in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

cat command in linux

If you’re familiar with Linux, you’ve almost certainly encountered a command or script that uses the cat command. 

Short for concatenate, the cat command is a versatile tool used to display the content of a file (or multiple files) directly to the standard output, bypassing the need to open files for editing. 

In this comprehensive guide, we will cover the basic syntax of the cat command and then go into the details of using the command to work with files and standard input. 

Table Of Contents

  1. An Overview of the cat Command
  2. Popular cat Command Options
  3. 15 Examples of cat Command in Linux
  4. Conclusion
  5. FAQs

An Overview of the cat Command

In a Linux environment, cat is used to handle a single file or multiple files, showing the file contents in the terminal. 

This command enables users to not only view the contents of a file but also create a new file and redirect output. It’s particularly useful for displaying file content that includes tab characters and seamlessly merging the contents of files. 

Whether you’re dealing with standard input or output, the cat command in Linux is an essential tool for managing and viewing file content.

The basic syntax of the cat command in Linux distributions is as follows:

# cat [OPTION]... [FILE]...

Here,

  • cat: This initiates the command, instructing the system to merge files or display them on the standard output.
  • [OPTION]…: This segment is optional and allows for the inclusion of various options to alter the cat command’s functionality.
  • [FILE]…: This part specifies the file or files you want to display. You can list multiple files separated by spaces. The contents of each specified file will be displayed in the terminal in the order they are listed in the command.

Popular cat Command Options

You can modify the standard behavior of the cat command by leveraging one or more of the following options.

popular cat command options

15 Examples of cat Command in Linux 

We will now demonstrate the flexibility and versatility of the cat command in the following examples.

The Prerequisites

You need the following before you can try out the examples we will mention below:

  • A system running a mainstream Linux distro
  • A user account with sudo or root privileges

Example #1: Create the Test File

We strongly recommend creating several test files with a few lines of text to use in the following examples.

We will use the cat command to show how to create these files – test1.txt and test2.txt

Launch the terminal and enter the following command: 

# cat >test1.txt

The terminal will display a new line where you can input your desired text. For instance, enter the following sentence.

This is test file #1.

Once you have entered your text, press and hold the Ctrl key, then tap the D key to save the content and exit the command.

This is test file #1

Repeat the steps to create test2.txt. Start by executing the command:

# cat >test2.txt

Next, enter your text. We entered the following:

This is test file #2.

Finally, finish by pressing Ctrl and D together.

This is test file #2

Example #2: Display the Contents of a Single File

Run the cat command with the file name to display the contents of the file in the terminal. For instance, run the following command to display the contents of test1.txt:

# cat test1.txt

The output will resemble the following screenshot:

This is test file #1

Example #3: Show the Contents of Various Files

To show the contents of multiple files, add the file names with the cat command. Note that the output will follow the order of the files in the command. For instance, to show the contents of test1.txt and test2.txt, run the following command:

# cat test1.txt test2.txt

cat test1.txt test2.txt

Example #4: Redirect the Output to a Single File

By default, the cat command displays its output in the terminal (the standard output). However, you can change this behavior and redirect the output to a file. 

For instance, run the following command to redirect the output of the cat command to a file named test3.txt:

# cat test1.txt > test3.txt

If the target file doesn’t exist, the cat command will create it. 

After the redirection, we recommend printing out the contents of the redirected file to verify that the operation has succeeded. For this, use the cat command to display the contents of test3.txt:

# cat test3.txt

The output shows that this file contains the contents of the test1.txt:

cat test3.txt

Note that the redirection replaces the existing contents of the target file. So exercise caution when redirecting to existing files. Consider the following redirection commands:

# cat test2.txt > test3.txt

# cat test3.txt

As you can see, the content of test3.txt is as follows:

cat test2.txt test3.txt cat test3.txt

Example #5: Route the Contents of Several Files

You can merge the contents of several files into a single file by redirecting the contents of the files in order. For instance, consider the following redirection pipeline:

# cat test1.txt test2.txt > test3.txt

Now, view the contents of the test3.txt with the following command:

# cat test3.txt

You can see that the output shows the merged contents of all the files:

cat test3.txt 2

Example #6: Display the Contents in Reverse Order

By default, cat starts displaying the file’s contents from the first entry. You can change this and show the contents of a file from the last line to the first by using the tac command:

# tac test3.txt

tac test3.txt

Example #7: Add the Contents of One File to Another

The cat command can append the contents of one file to the end of another. For this, use the >> instead of the more traditional > for redirection. Unlike the redirection we mentioned earlier, this method preserves the original contents of the target file.

For instance, consider the following command that adds the contents of the test1.txt to test3.txt: 

# cat test1.txt >> test3.txt

To open the test3 file, execute the following command:

# cat test3.txt

As you can see, the contents of the test3 are followed by the contents of the test1.

cat test3.txt 3

Example #8: Add Text to an Existing Document

You can append text to a file that already exists on the system by running the following command:

# cat >> test1.txt

You will get the blinking cursor that indicates that you can now add your content. We added the following line to the file:

This is the second line in test file #1.

Press and hold Ctrl and D to exit the command. 

To verify the contents of the test1.txt file, you can use the following command:

# cat test1.txt

cat test1.txt

Example #9: Merge and Redirect

You can combine the merge and add functionalities of the cat command in a single command. For instance, you can merge the content of two files and store the unified output into a new file by running this command:

# cat test1.txt test2.txt > test4.txt

You should verify the success of the operation by viewing the contents of the test4.txt file:

# cat test4.txt

cat test4.txt

Alternatively, it’s possible to add the contents of several files to the end of an existing file with the following command:

# cat test2.txt test1.txt >> test4.txt

Use the following command to view the contents of the test4.txt file:

# cat test4.txt

Note the sequence in which files are listed in the cat command is the same sequence in which they will be appended to the target file.

cat test4.txt 2

Example #10: More and Less Options to Manage Large Files

When using the cat command on a very large file, you won’t see the entire content and miss out on several screenfuls of data. 

You can manage this situation by paginating the output. This means piping the output of the cat command into more command:

# cat test4.txt | more

This command shows one page of the file at a time. By pressing a key, you can move to the next page.

If you need the flexibility to scroll both forwards and backward in the file’s content, pipe the output of the cat command into the less command:

# cat test4.txt | less

Example #11: Show Line Numbers

By default, cat doesn’t show line numbers. 

For better readability, especially with large files, including line numbers in the output is helpful in tracking your position and locating specific portions. For this, add the -n option to the cat command:

# cat –n test1.txt

You’ll see the output as shown in the following image:

cat –n test1.txt

Example #12: Show the End of Line

You can show the end of each line of the file. The cat command can append a $ symbol at the end of each line when you add the -e option. The syntax of the command is as follows:

# cat -e test1.txt

cat -e test1.txt

Example #13: Show TAB Separated Lines

The cat command can show tab spaces in the text by presenting the file’s contents.

For demonstrating tab-separated lines, use the -t option: 

# cat -t test4.txt

Note that ^I denotes the tab spaces in the text.

Example #14: Remove Blank Lines

You can compress the output of a large file by removing empty lines by using the -s option:

# cat -s test4.txt

Example#15: Get Help With the cat Command

If you find it challenging to recall the options, consider using the –help option with the command:

# cat ––help

cat ––help

Also Read: lsof Command in Linux with Examples

Also Read: Mastering the Linux tee Command for Redirecting Output In 9 Examples

Conclusion

Mastering the cat command in Linux is an essential skill for anyone working in a Unix environment, offering a powerful tool for handling files. 

From basic usage like viewing a file on screen, creating a file, and displaying file content to more advanced operations involving input files, file arguments, and output devices, cat stands out for its versatility. 

The command’s ability to use redirection operators, such as the > and >>, enhances its functionality, allowing for the concatenation of file contents and more. 

If you’re looking for a robust server for your Linux projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the cat command in Linux used for?

The cat command in Linux is used to display the contents of a file, create new files, and concatenate files.

Q. How can I create a file using the cat command?

To create a new file using the cat command in Linux, you can use the command followed by the name of the file you want to create. For example, `cat > newfile.txt`.

Q. How do I display the contents of a file using the cat command?

You can display the content of a file by using the cat command followed by the name of the file. For example, `cat filename.txt`.

Q. What is the difference between cat command and less command in Linux?

The cat command is used to concatenate and display file contents in Linux, while the less command is used to view the content of a file page by page.

Q. How can I redirect the output of the cat command to another file in Linux?

To redirect the output of the cat command to another file, you can use the `>` symbol followed by the name of the new file. For example, `cat file1.txt > file2.txt`.

Q. Can the cat command be used to concatenate multiple files in Linux?

The cat command can concatenate multiple files in Linux by providing the file names as arguments. For example, `cat file1.txt file2.txt > combinedfile.txt`.

Q. What is the meaning of cat -n and cat -e commands in Linux?

The `cat -n` command in Linux displays the line numbers along with the file content, while the `cat -e` command displays the end-of-line characters, making reading and analyzing the content easier.

Try this guide with our instant dedicated server for as low as 40 Euros