How to Use the Linux Command cat and Its 5 Options

The Linux Command cat is used to display the contents of a file on the screen or to combine multiple files into a single one. In this post, we’ll explore the basic usage of the cat command, its various options, and provide some tips and precautions for using it effectively in practical scenarios.

What is the cat Command?

The cat command stands for “concatenate,” meaning “to link together.” In Linux, the cat command is used to display the contents of a file or to concatenate (link) multiple files. The simplest use case is to check the contents of a file. This is the most basic function of the cat command, which outputs the contents of a file directly.

cat filename
ShellScript

The command above displays the contents of the specified file on the terminal screen. The output includes all the content of the file and is displayed at once without any page breaks.

Figure 1. Linux Command cat: Execution Result
Figure 1. Linux Command cat: Execution Result

Main Options of the cat Command

The cat command offers various options beyond its basic functionality, allowing for more flexible use depending on the user’s needs. Let’s explore the main options one by one.

-n Option: Display Line Numbers

The -n option adds line numbers at the beginning of each line when displaying the file contents. This is especially useful when reviewing code or log files, as it makes it easier to reference specific lines.

cat -n filename
ShellScript

Using this command, the file contents are displayed with line numbers, as shown in the figure below.

Figure 2. Linux Command cat: Display Line Numbers with -n Option
Figure 2. Linux Command cat: Display Line Numbers with -n Option

-b Option: Display Line Numbers Only on Non-Empty Lines

Similar to the -n option, the -b option numbers only non-empty lines. Empty lines are skipped, making this option handy when dealing with files that contain many blank lines.

cat -b filename
ShellScript

With this command, the output skips numbering blank lines and only numbers lines with content, as illustrated in the figure below.

Figure 3. Linux Command cat: Exclude Line Numbers on Empty Lines with -b Option
Figure 3. Linux Command cat: Exclude Line Numbers on Empty Lines with -b Option

-E Option: Show Dollar Sign ($) at the End of Each Line

The -E option adds a dollar sign ($) at the end of each line, which is helpful when you want to clearly distinguish the end of each line. This is particularly useful for identifying trailing spaces or tabs in lines.

cat -E filename
ShellScript

When you run this command, a dollar sign ($) appears at the end of each line, allowing you to easily verify the line ends. For example, in line 3, you can quickly see that there is a space at the end of the line.

Figure 4. Linux Command cat: Display Line Ends with Dollar Sign ($) Using -E Option
Figure 4. Linux Command cat: Display Line Ends with Dollar Sign ($) Using -E Option

-s Option: Compress Consecutive Empty Lines

If a file contains many consecutive empty lines, you can use the -s option to compress these into a single empty line. This improves the readability of the file.

cat -s filename
ShellScript

Using this command, consecutive empty lines are compressed into a single line. As shown in the figure below, lines 4 and 5 are empty with only the -n option. However, with the -s option, the two empty lines are reduced to one.

그림 5. 리눅스 명령어 cat: -s 옵션으로 여러 개의 빈 줄 한 줄로 표시
그림 5. 리눅스 명령어 cat: -s 옵션으로 여러 개의 빈 줄 한 줄로 표시

Figure 5. Linux Command cat: Compress Multiple Empty Lines with -s Option

-T Option: Show Tab Characters as ^I

Tab characters are not visible on the screen, so it may be difficult to distinguish tabs from spaces in a file. The -T option shows tab characters as ^I, allowing you to visually identify them.

cat -T filename
ShellScript

When you run this command, tab characters in the file are displayed as ^I, as shown in the figure below.

Figure 6. Linux Command cat: Display Tab Characters as ^I with -T Option
Figure 6. Linux Command cat: Display Tab Characters as ^I with -T Option

Examples of Using the cat Command

The cat command is not only for displaying file contents but can also be used in various other situations. Let’s look at some practical examples.

Combining Multiple Files into One

If you want to combine the contents of multiple files into a single file, the cat command makes this easy. Here’s an example of combining two files (file1.txt and file2.txt) into one (output.txt).

cat file1.txt file2.txt > output.txt
ShellScript

In the figure below, files a.txt and b.txt are created, and then the two files are concatenated into c.txt.

Figure 7. Linux Command cat: Combining Files
Figure 7. Linux Command cat: Combining Files

Saving Input from the Terminal to a File

The cat command can also be used to save content entered directly in the terminal to a file.

cat > new_file.txt
ShellScript

After entering this command, type your content, and when you’re done, press Ctrl + D to save the input into the file new_file.txt. In the figure below, the user inputs “Hello, World!” and saves it to d.txt, then verifies the content.

Figure 8. Linux Command cat: Saving Terminal Input to a File
Figure 8. Linux Command cat: Saving Terminal Input to a File

Precautions When Using the cat Command

The cat command is very useful, but there are a few things to watch out for.

  • Be cautious with large files: Displaying large files with the cat command can flood the terminal with content, making it difficult to perform other tasks until the output completes. In such cases, using the less or more command is more appropriate.
  • Be careful with file overwriting: When combining multiple files with the cat command, if you specify an existing file name, the file will be overwritten. This could result in the loss of important files, so caution is necessary.

Summary

The Linux command cat is an invaluable tool for managing files in Linux. It allows you to display file contents, combine multiple files, and analyze files more easily with options like displaying line numbers or tab characters. However, caution is required when dealing with large files or overwriting existing files. By effectively using the Linux command cat, you can perform Linux tasks more efficiently. We hope this guide helps you make the most of the Linux command cat!

References

Leave a Comment