The Ultimate Guide to Linux cat Command - From Terminal File Viewing to Merging and Creation Tips!

Overview

This guide covers everything from the basics to practical applications of the cat command, one of the most widely used commands in Linux and Ubuntu terminals. Beyond simply displaying file contents, we introduce features such as displaying line numbers, visualizing whitespace/non-printing characters, file concatenation, and creating files using EOF (Heredoc) along with practical examples.

1. Basic Usage of cat Command and Instantly Viewing Text File Contents

Run the command echo "Hello Ubuntu 24.04 LTS World!" > sample.txt to create a sample text file to be used for practicing the cat command.

echo “Hello Ubuntu 24.04 LTS World!” > sample.txt

The cat command outputs the entire contents of a specified text file to the terminal screen.
Running the command cat sample.txt immediately displays the contents written in the file on the screen.
⚙️ [Key Options]

-n : Displays line numbers for all output lines.
-b : Displays line numbers only for non-blank lines.
-s : Squeezes multiple consecutive blank lines into a single blank line.
-E : Displays a $ character at the end of each line to visualize line breaks.
-T : Converts tab (TAB) characters to ^I for display.
-A : Visualizes all elements inside the file, including tabs, line breaks, and control characters.

cat sample.txt

2. How to Use Line Number Display (-n) and Special Character Visualization (-A) Options

Run the command echo -e "First Line\nSecond Line\n\nFourth Line\tTabbed" > options_demo.txt to prepare a sample file for practicing cat command options.

echo -e “First Line\nSecond Line\n\nFourth Line\tTabbed” > options_demo.txt

Running the command cat -n options_demo.txt outputs all lines in the file with line numbers attached, including blank lines.
This allows you to quickly identify exact line positions at a glance when navigating code or configuration files.

cat -n options_demo.txt

Running the command cat -A options_demo.txt visualizes all invisible control characters, such as tabs and line breaks.
A $ character is appended to the end of each line, and tab (TAB) locations are displayed as ^I, enabling you to clearly identify special characters or formatting errors within the file.

cat -A options_demo.txt

3. Combining Multiple Text Files (Concatenate) and Saving to a New File

Run the command echo "[Server Config]" > part1.txt to create part1.txt, the first configuration file to be used for practicing file concatenation with the cat command.

echo “[Server Config]” > part1.txt

Next, run the command echo "Port=8080" > part2.txt to create part2.txt, the second configuration file to be merged.

echo “Port=8080” > part2.txt

Run the command cat part1.txt part2.txt > combined_config.txt to concatenate the contents of the two files in sequence.
Using the redirection symbol (>) allows the merged result to be directly saved as a new file named combined_config.txt instead of displaying it on the terminal screen.

cat part1.txt part2.txt > combined_config.txt

Run the command cat combined_config.txt to check the full contents of the merged file.
You can see that [Server Config] from part1.txt and Port=8080 from part2.txt have been sequentially combined and saved into the file.

cat combined_config.txt

4. Creating Multi-line Files in the Terminal Without an Editor (Heredoc / EOF)

Run the command cat << 'EOF' > heredoc_demo.txt to create a multi-line text file named heredoc_demo.txt directly in the terminal without opening a separate text editor.
Using the Heredoc (<< 'EOF') syntax together with the redirection symbol (>), you can immediately save the typed text into a file until the EOF delimiter is entered.

cat « ‘EOF’ > heredoc_demo.txt
Line 1: Server Setup
Line 2: Firewall Config
EOF

Run the command cat heredoc_demo.txt to verify the complete contents of the file created on the fly with Heredoc.
You can see that the entered lines Line 1: Server Setup and Line 2: Firewall Config are properly saved in the file and printed on the screen.

cat heredoc_demo.txt

5. Limitations of cat with Large Files and Combining Alternative Commands (head, tail) via Pipes

Run the command seq 1 100 > large_log.txt to generate a sample log file for practicing large file navigation.

seq 1 100 > large_log.txt

Running the command head -n 5 large_log.txt outputs only the top 5 lines starting from the first line of the specified file to the terminal screen.
Unlike the cat command, it does not output the entire content of a large file, making it useful for quickly inspecting the file header structure or starting section.

head -n 5 large_log.txt

Running the command tail -n 5 large_log.txt selectively outputs only the bottom 5 lines from the end of the specified file.
Without needing to open the entire file, you can efficiently check the latest updated logs or ending records.

tail -n 5 large_log.txt

Run the command cat large_log.txt | grep -E "(10|50|100)" to filter and output only the lines containing the specified patterns 10, 50, and 100 from the file content.
By piping (|) the terminal output retrieved by cat into the grep command, you can quickly search for lines containing specific keywords within large files.

cat large_log.txt | grep -E “(10|50|100)”

So far, we have taken a look at useful tips for using the Linux cat command, from file viewing to merging and file creation.
We hope you refer to the various techniques shared above to create a more efficient Linux terminal environment.