There are multiple ways to read the content of a file in Linux. It includes GUI and CLI, and one of the most frequently used is the cat command.

What is Cat Command?

Cat stands for concatenating, which helps us read single or multiple file content. We can also create a new file by adding some fresh new content using the cat command.

The general syntax of Cat Command looks like the one below.

  • cat [OPTION] [FILE]

Today, we will learn different ways to use the cat command in Linux with examples.

1. Display the Contents of the File

Below is an example of displaying the file's content using the cat command.

  • cat file.txt
  • Hello UBUNTUSHELL!

2. Create a File with the Cat Command

Mostly, the touch command is recommended to create a new file, but you can use the cat command to do the same job as shown below.

  • cat > file.txt

The above command creates a new file with the name "file.txt" and asks you to add content. Once you are finished adding content, press the CTRL+D shortcut key to save the file.

If you want to avoid removing old content and appending new content, use the double >> redirection symbol as shown below.

  • cat >> file.txt

3. Display the Content of Multiple Files

If you have multiple files in the same directory, you can specify each one of them with a space in between them to output the file’s content.

  • cat file1.txt file2.txt
  • I am from file1.txt
  • I am from file2.txt

If there are many files sharing the same "txt" extension, you can use the below command to output all the file contents with the "txt" extension.

  • cat *.txt
  • I am from file1.txt
  • I am from file2.txt
  • I am from file3.txt

4. Redirect Output to a New File

If you want to create a new file with multiple existing files, you can use the redirection symbol to output the content to a new file, as shown below.

  • cat file1.txt file2.txt file3.txt > file4.txt
  • cat file4.txt
  • I am from file1.txt
  • I am from file2.txt
  • I am from file3.txt

If there are many files sharing the same "txt" extension, you can use the below command to output all the file content with the "txt" extension in a new file.

  • cat *.txt > file4.txt
  • cat file4.txt
  • I am from file1.txt
  • I am from file2.txt
  • I am from file3.txt

5. Remove Repeated Space

In the configuration file, you may find multiple spaces in between lines. All those extra spaces can be irritating while scrolling over the content.

All the extra spaces can be removed using -s flags, as shown below.

  • cat -s file.txt
  • Hello
  • UBUNTU
  • SHELL
  • Readers

You might not notice the difference, but there are three lines of space between the content that are suppressed to one line of space in the output.

6. Use the Cat Command with More and Less Options

If you have a file with a vast amount of content, you may have noticed that it's too boring to scroll.

With the help of more and less commands, you can display the single-screen content of the file by going down rapidly using the down arrow key.

  • cat file.txt | more
  • cat file.txt | less

7. Display Line Number in the File

In some cases, you might want to display the line number in front of each line in the file. As shown below, that can be done using -n flags.

  • cat -n file.txt
  • 1 HELLO
  • 2 UBUNTU
  • 3 SHELL
  • 4 READER

8. Display $ at the End of the Line

The extensive line that is often switched to the new line makes you confused about where the line is ending. As shown below, you can place "$" at the end of each line using the -e flag to avoid that confusion.

  • cat -e file.txt
  • HELLO$
  • UBUNTU$
  • READER$
  • Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.$

9. Sorting File Content

If your file content is unsorted, you can sort it by piping the sort command with the cat, as shown below.

  • cat file.txt | sort
  • 1
  • 2
  • 3
  • 4
  • 5

10. Display Content in Reverse Order

If you are just interested in the last content of the file, then you can use the tac command. The working of the tac command is similar to the cat command, except it displays the last content of the file first, as shown below.

  • tac file.txt
  • line number five
  • line number four
  • line number three
  • line number two
  • line number one

If the file is too big, you can use less commands along with tac, as shown below.

  • tac file.txt | less

11. Display Tab Separated Lines in File

In the below output, you can see that the lines that are separated using tabs have a "^" symbol at the beginning of them.

  • cat -T file.txt
  • Hello,^IUbuntu Shell Reader