Ubuntu Text Processing: Using the cat Command to View File Contents

1. What is the cat command?

In the Ubuntu system, the cat command is a fundamental and commonly used text processing tool. Its name derives from “concatenate” (meaning “to connect”), but in daily usage, we primarily use it to view the contents of files. Imagine that when you double-click to open a text file in the Windows system, the cat command is equivalent to “opening” the file in the Ubuntu terminal and directly displaying its contents—this is very intuitive.

2. Basic Usage: Viewing the Content of a Single File

The syntax for using the cat command to view file contents is very simple. Just enter the following in the terminal:

cat 文件名

Example: Suppose you have a file named test.txt with the following content (you can create a test file first using nano test.txt, enter a few lines of text, and save it):

Hello, this is a test file.
It has three lines.
The third line is here.

Executing cat test.txt in the terminal will directly display the file content on the screen:

Hello, this is a test file.
It has three lines.
The third line is here.

3. Common Options: Making cat More Flexible

The cat command supports several options (parameters) that can adjust the output format, making content viewing more convenient. Here are the most commonly used options for beginners:

3.1 Show Line Numbers (-n option)

If you want to number every line of the file (including empty lines), use the -n option:

cat -n 文件名

Example: If test.txt contains a mix of empty lines and content (assume the content is as follows):

Line 1: First line.

Line 3: Third line.

After executing cat -n test.txt, the output will display line numbers for all lines:

     1  Line 1: First line.
     2  
     3  
     4  Line 3: Third line.
     5  

3.2 Show Line Numbers for Non-Empty Lines (-b option)

-b is similar to -n, but only numbers non-empty lines; empty lines will not be numbered:

cat -b 文件名

Using the same example as above, after executing cat -b test.txt:

     1  Line 1: First line.
     2  
     3  Line 3: Third line.
     4  

3.3 Compress Consecutive Empty Lines (-s option)

If the file has many consecutive empty lines (e.g., multiple newline characters), it will look messy. The -s option can merge multiple consecutive empty lines into a single one:

cat -s 文件名

Example: Suppose the file content has multiple empty lines:

Line 1.


Line 3.

After executing cat -s test.txt, the output will be simplified to:

Line 1.

Line 3.

4. Handling Multiple Files: Merging Content

cat can not only view single files but also display the contents of multiple files simultaneously and output them in sequence. For example, if you have two files, file1.txt and file2.txt:
- Content of file1.txt: Hello from file1!
- Content of file2.txt: Hello from file2!

Executing cat file1.txt file2.txt will output:

Hello from file1!
Hello from file2!

If you want to merge the contents of multiple files into a new file (this will overwrite the original content; note that backup is recommended!), use the redirection symbol >:

cat file1.txt file2.txt > combined.txt

After execution, combined.txt will contain all the content from file1.txt and file2.txt.

5. Common Issues and Precautions

  1. File Does Not Exist?
    If the entered filename does not exist, cat will report an error: cat: 文件名: No such file or directory. In this case, check if the filename is spelled correctly or if the file is in the current directory (you can use the ls command to confirm if the file exists).

  2. Insufficient Permissions?
    If the file does not have read permissions, you will get a Permission denied error. For example, system files like /etc/passwd usually require administrator privileges. A regular user can try using sudo cat (but note: do not modify system files unless necessary).

  3. Risk of Overwriting with Redirection?
    Using > will directly overwrite the target file. For example, cat a.txt > b.txt will clear the original content of b.txt and only retain the content of a.txt. To append content instead, use >> (but still be cautious).

Summary

The cat command is an entry-level tool for text processing in Ubuntu. It is simple yet practical, ranging from viewing single files, outputting with line numbers, to merging multiple files. By practicing with several examples (e.g., creating test files and testing different options), you will quickly master its usage!

Xiaoye