Examples to Use cat Command in Linux

Helpful Guide to Use cat Command in Linux with Examples

For efficient Linux system management, automation, troubleshooting, security, customization based on needs, effective use of resources, comprehensive learning of Linux commands, and gaining skills in using Linux terminal are necessary. Understanding and utilizing common Linux commands enables users to manage their Linux systems effectively, and it facilitates essential tasks such as configuring system settings, managing files and directories, installing software, and monitoring the performance of their Linux system.

The cat (short for concatenate) command in Linux is a versatile and fundamental command-line tool that is executed to manipulate the file. The primary function of the cat command in Linux is to display and concatenate the contents of files. However, it is also used for creating new files, displaying line numbers, redirecting output in the terminal or files, and so on.

The functionality of the cat command extends when combined with other Linux commands, allowing it to serve objectives such as the conversion of file formats to binary or hexadecimal and page navigation, etc. Fortunately, all Linux distributions support the cat command by default, so you can use its capabilities without needing installation. In this article, you will learn how to use cat command in Linux Ubuntu, Debian, CentOS and etc. If you’re ready to become more proficient in Linux management, let’s get started and gain a new experience with Linux.

Prerequisite to Using Linux cat Command

  • Linux VPS or Linux system
  • Access to the Linux terminal (using Ctrl + Alt + T shortcut key)

cat command Syntax in Linux

The main syntax of the cat command to use is as follows:

cat [Options] [Filename(s)]

Options: The ‘cat’ command provides various options to modify its behavior, which you can use for different purposes.

Filename: Specify the name of the file (or files) to be manipulated by the cat command.

cat command Options in Linux

The cat command in Linux supports a wide range of options to extend its functionality. Here are some common options used with the cat command:

OptionsDescription
-b(number nonempty output lines)Displays and numbers nonempty lines.
-n(number all output lines)Displays the entire content of the file with numbering at the beginning of each line.
-s(squeeze multiple blank lines into one)Squeezes multiple blank lines into one. It preserves one blank line from repeated blank lines.
-A(equivalent to -vET)Displays non-printing characters (tabs, ends of lines, and control characters) with visible representations.
-E(display $ at the end of each line)The highlights line ends by displaying the $ character at the end of each line.
-T(display TAB characters as ^I)Displays TAB characters as ^I.
-v(display non-printing characters)Displays non-printing characters as ^X or M-X.
--versionProvides information about the version.
--helpProvides information and details about the available options for the 'cat' command and how to use 'cat'.

How to Use the cat Command in Linux

You have become familiar with the basic syntax of the ‘cat’ command and its options, but that’s not enough. You must learn how to use the cat command for various purposes. Stay with us until the end of the article to learn about the most common use cases of the ‘cat’ command.

Displaying the contents of a single file

Various commands are used to view the contents of a file, and the cat command is one of the most common and efficient among them. The main purpose of running the cat command is to display the contents of a file. To do this, simply specify the filename in the following command:

cat filename

For example:

cat Opera.txt

To save time and enhance navigation in lengthy command outputs, you can combine the cat command with the less and more commands to paginate the output when navigating large files. To view the contents of a text file and browse the output page by page, you can use the following commands:

cat filename | less

Or

cat filename | more

The pipe “|” symbol in the previous command is a symbol that sends the output of the cat command as input to the more and less commands to optimize the navigation within the printed content and provide scroll and search capabilities using keyboard keys.

Using the less and more commands to view large files or the output of commands that generate long and extensive text is useful. Therefore, by combining the cat command with the less and more commands, you can navigate and read the contents of a text file in a controlled manner. The less command supports more advanced navigation capabilities compared to the more command. The less command allows scrolling both backward and forward in the displayed content, that the more command has a weaker performance in this field.

cat command for displaying Multiple Files

The cat command allows you to view the contents of multiple files simultaneously. To achieve this, simply specify the files with spaces between them. To display the contents of multiple text files, run the following command:

cat file1 file2 file3

For example:

cat Opera1.txt Opera2.txt Opera3.txt

The previous command’s output displays the contents of 3 files at once.

 Displaying all available files’ content with cat command in linux

The cat command allows users to view the content of all files in the current directory. Additionally, the cat command, using the “*” wildcard, can display the content of a specific type of file in the current directory. For example, to view the content of all files with the .txt extension in the current directory, use the following command:

cat *.txt

As a result, you can check the contents of all txt files simultaneously.

Displaying line numbers with the contents through cat command

Numbering lines is effective in facilitating the examination of the content of large files. The cat command, when executed with the “-n” option, prints the content of files with line numbers. Therefore, to view the content of a file with line numbers printed before each line of the file’s content, use the following command:

cat -n filename

Suppose we have a file called “example.txt” with the following contents:

This is the first line.
This is the second line.
This is the third line.

After running the cat -n example.txt command, the following output will be produced:

1 This is the first line.
2 This is the second line.
3  This is the third line.

Furthermore, the “cat” command provides the capability to combine the content of multiple files and number the lines of content from these files. To achieve this, use the following example:

cat -n example1.txt example2.txt

The output of this command concatenates the contents of the “example1.txt” and “example2.txt” files and adds numbers at the beginning of each line of output content.

Here’s an example assuming the contents of ” example1.txt ” and ” example2.txt “:

example1.txt:

This is example file 1.
It contains information about the server.
Server information: IP address - 192.168.1.1, Port - 8080

example2.txt:

This is example file 2.
It also contains information about the server.
Server information: IP address - 192.168.1.2, Port - 9090

By running the cat -n example1.txt example2.txt command, the following output is displayed:

1  This is example file 1.
2  It contains information about the server.
3  Server information: IP address - 192.168.1.1, Port - 8080
4
5  This is example file 2.
6  It also contains information about the server.
7  Server information: IP address - 192.168.1.2, Port – 9090

Displaying and numbering Non-Empty Lines in Linux

To view and number non-empty lines in the content of a file, you can use the “-b” option in conjunction with the cat command:

cat -b filename.txt

Note that the “-b” option overrides the function of the “-n” option.

Squeezing multiple empty lines into a single line

Displaying repeated empty lines in the output of commands or file content can be annoying. To make the content of a file or command output more manageable, you can use the “-s” option along with the cat command. The “-s” option removes repeated empty lines in the file content and squeezes these lines into a single blank line, meaning it preserves one empty line from all consecutive empty lines and removes the rest. To achieve this, run the following command:

cat -s filename.txt

Viewing File Content with Highlighting Line Ends

To highlight the line endings in the content of a file that represents non-printing characters, you can use the “-E” option, which highlights the line endings with the symbol ($). You can instruct the cat command to highlight the line endings in the file content using the following command :

cat -E filename.txt

Output:

This is the first line.$
This is the second line.$

Displaying Non-Printing Characters

If you want to display non-printing characters (tabs, line feeds, etc.) in the content of a file, you can execute the cat command with the “-v” option:

cat -v filename.txt

The output of this command makes all non-printing characters visible. To view the non-printable tab character in the content of a file, you can use the “-T” option along with the cat command to display (^I) instead of spaces in place of tab characters in the file’s content:

cat -T filename.txt

Output:

This ^I is^I the ^I second^I line.

Displaying the Start of Files content

To see the first few lines of the file content in linux, combine the cat command with head:

cat filename.txt | head

Displaying the End of Files content

Combining the cat command with the tail command will display the last few lines of a file. For this purpose, run the following command:

cat filename.txt | tail

Reverse order display of a file’s contents

Some users need to examine the content at the end of a file, which has recently been added, and viewing the content in reverse order, i.e., from the last line to the first, is important to them. For this purpose, you can use the tac command (which is cat in reverse) instead of the cat command:

tac filename.txt

The lines at the end of the file are displayed first, and the lines at the beginning are displayed last when using this command.

Viewing a specific range of lines from a file

Combining the cat command with the “sed” command enhances efficiency and allows users to view specific ranges and lines of a file’s content without wasting time, thereby simplifying and speeding up their tasks. For example, to view lines 15 to 20 of the content in the example.txt file, run the following command:

cat example.txt | sed -n '15,20p'

Creating New Files with cat command

The primary function of the “cat” command is to display the content of a file, but it also has the capability to create new files. Therefore, users can use the cat command along with the “>” symbol (known as the “output redirection operator”) to create a new file and input text into it. To achieve this, use the following example:

cat > newfile

After executing this command and seeing the blinking cursor on a new line, type the text you desire, and to save and exit, press Ctrl+D.

Note: When creating a file, be cautious that the specified filename doesn’t already exist on your system because if a file with that name already exists, this command will overwrite the existing file’s content.

Using cat command for Appending Text to a File

As mentioned at the beginning of the article describing the functionality of the cat command, in addition to displaying file contents, the cat command is also helpful for file manipulation. One of the uses of the cat command is to append content and text to the end of an existing file. To  append text to the end of the existing file’s content, use the “>>” symbol (known as the “append redirection operator”) along with the cat command as follows:

cat >>filename.txt

After executing the previous command, enter the text you want to append to a specific file and to save and exit, press Ctrl+D. To confirm the appending of text to the existing file, you can view the content of the desired file by running the cat command:

cat filename.txt

Appending File Contents to Another File via cat command

In some situations, users need to copy one file’s contents into another for various purposes. The cat command, along with the “>>” symbol, meets your needs for this task:

cat example1.txt >>example3.txt

By running this command, the content of “example1.txt” will be appended to the end of the content in “example3.txt.”

If you want to create a copy of the contents of one file in a new file or a different file, you should use the “>” symbol:

cat example1.txt > example3.txt

By executing the previous command, the content of “example1.txt” will be located to “example3.txt.” If “example3.txt” did not exist before, a file with that name will be created, and the content of “example1.txt” will be placed in it.

Concatenating Files

The cat command can merge the content of multiple files and output the combined content into a new file. To achieve this, use the following command:

cat file1 file2 > combined_file

This command reads the content of both “file1” and “file2,” merges them together, and then saves the combined content in a new file named “combined_file.”

By viewing the “combined_file” content, you can ensure that your changes have been applied.

Sorting contents of file Alphabetically

As you may know, the cat command can be used to pipe the output of one file to another command to expand functionality and perform further processing. Therefore, you can combine the cat command with “sort” to sort the lines of content alphabetically:

cat -v filename.txt | sort

Viewing the content of a file in binary format

The cat command, when combined with “hexdump” or “xxd” commands, leads to displaying the content of a file in hexadecimal or binary format. The “xxd” tool is usually pre-installed on most Linux distributions, so to view the content of a file in binary format, use the following command:

cat filename | xxd -b

The output displays the file’s contents side-by-side in their original and binary forms.

Alternatively, you can use hexdump to achieve a similar result:

cat filename | hexdump -C

The combination of the cat and “hexdump” commands for displaying the content of a file in binary format presents the file’s content in a format that shows both the hexadecimal and ASCII representations.

Conclusion

In this article, you’ve gained skills in using one of the most common Linux commands, the cat command. You’ve learned how to use the cat command to view the content of files as needed, make modifications to files, create new files, and concatenate the contents of files. Theman catcommand can guide you to more advanced operations by providing additional options and comprehensive information about the “cat” command.

We hope that through reading this article, your skills and knowledge in using Linux commands have expanded. Feel free to ask any questions or give your thoughts on the post in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked.