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.
Table of Contents
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
ShellScriptThe 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.
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
ShellScriptUsing this command, the file contents are displayed with line numbers, as shown in the figure below.
-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
ShellScriptWith this command, the output skips numbering blank lines and only numbers lines with content, as illustrated in the figure below.
-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
ShellScriptWhen 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.
-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
ShellScriptUsing 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.
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
ShellScriptWhen you run this command, tab characters in the file are displayed as ^I
, as shown in the figure below.
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
ShellScriptIn the figure below, files a.txt
and b.txt
are created, and then the two files are concatenated into c.txt
.
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
ShellScriptAfter 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.
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
ormore
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!