How to Use the Cat Command in Linux
cat Command Options
The cat command is a powerful and flexible utility in Linux. It allows you to view, concatenate, and manipulate file contents. The name cat stands for concatenate, reflecting its original purpose of merging files into a single output stream. While many users rely on cat to display files in the terminal, its options enable a variety of additional use cases, such as formatting, debugging, and stream editing.
This section explains the command syntax, describes common flags, and demonstrates practical examples.
Command Syntax
The cat command derives from the word concatenate, emphasizing its design to merge file contents into a single output. While concatenation remains its core functionality, cat is commonly used to display files, inspect content, and pipe data between commands.
The general syntax of the cat command is:
cat [options] [file(s)]
[options]: Flags that modifycat‘s behavior. Use these to change output formatting or processing logic.[file(s)]: One or more files to process. If omitted,catreads from standard input.
When you run cat without arguments, it enters interactive mode. Type input and press Ctrl + D to end. This behavior is useful for quick notes or piping input manually.
Summary of Common Options
The table below summarizes the most commonly used cat options.
| Option | Description | Example |
|---|---|---|
-b |
Number non-empty output lines | cat -b example.txt |
-s |
Squeeze multiple blank lines | cat -s example.txt |
-E |
Show $ at end of each line |
cat -E example.txt |
-T |
Show tabs as ^I |
cat -T tabbed.txt |
-A |
Equivalent to -vET |
cat -A example.txt |
-v |
Show non-printing characters | cat -v ctrlc.txt |
Demonstrating Selected Options
The cat command supports several options that enhance its functionality. Below examples show how they work in practice.
Prepare the Test File
- Create and open a file named
example.txt:console$ nano example.txt - Add the following content to the file:
Hello, everyone! Greetings from Vultr! - Save and exit using Ctrl + X, then press Y and Enter.
Try These cat Options
-b: Numbers only non-empty lines. Useful for code or logs where spacing matters less than content.console$ cat -b example.txtOutput:
1 Hello, everyone! 2 Greetings from Vultr!!-s: Squeezes multiple consecutive blank lines into a single one. Helps tidy up cluttered files.console$ cat -s example.txt-E: Adds a$at the end of each line to highlight line endings or trailing whitespace.console$ cat -E example.txtOutput:
Hello!$ $ Greetings from Vultr!$-T: Shows tab characters as^I, making it easier to spot indentation issues. You can create a file with tabs like this:console$ echo -e "Hello,\tWorld!" > tabbed.txt $ cat -T tabbed.txt
Output:
Hello,^IWorld!-A: Combines-E,-T, and-vto show line endings, tabs, and non-printable characters all at once.console$ cat -A example.txtOutput:
Hello,^Ieveryone!$ $ Greetings from Vultr!$-v: Reveals non-printing characters like control codes. For example:console$ echo $'\x03' > ctrlc.txt $ cat -v ctrlc.txt
Output:
^C
Display Files Using the cat Command
The cat command is most commonly used to display the contents of files directly in the terminal. It’s a simple and fast way to view text files such as logs, scripts, or configuration files without opening a text editor. This section demonstrates how to display single files, multiple files, and efficiently view large files.
- Create a file named
tutorial.txtwith a single line of content:console$ echo "Learn cat commands with Vultr!" > tutorial.txt
- Display the contents of
tutorial.txt:console$ cat tutorial.txtOutput:
Learn cat commands with Vultr! - Display multiple files by listing them after the
catcommand. For example, to viewexample.txtfollowed bytutorial.txt:console$ cat example.txt tutorial.txtOutput:
Hello, everyone! Greetings from Vultr! Learn cat commands with Vultr!The display order depends on the sequence of file names. To reverse it, use the
taccommand instead ofcat. Thetaccommand is simplycatspelled backward. It displays files line-by-line in reverse order. - Use a wildcard (
*) to display all.txtfiles in the current directory:console$ cat *.txtTo read
.txtfiles from a specific directory, you can specify the path to the directory before the wildcard character.console$ cat /path/to/directory/*.txtReplace
/path/to/directory/with the actual file path. - To view large files without flooding the terminal, combine
catwith other commands using pipes.- Pipe
catwithheadto read from the top of the file. For example, to read the first 20 lines of a file namedlargefile.txt:console$ cat largefile.txt | head -n 20
- Similarly, pipe
catwithtailto read from the end of the file.console$ cat largefile.txt | tail -n 20
Replace
largefile.txtwith a large file of your choice. - You can also use the
lesscommand to view the contents in a scrollable window.console$ cat tutorial.txt | less
- Pipe
Combine Files in Linux using cat
True to its “concatenate” namesake, cat excels at merging files into unified outputs. This capability is invaluable for log analysis, configuration management, and batch text processing. Explore workflows ranging from basic merging to advanced stream manipulation below:
- Merge two files into a single file. The content would be arranged in the new file in order of how the files were listed using the
catcommand. In this example, mergeexample.txttotutorial.txtand write toresult.txt.console$ cat example.txt tutorial.txt > result.txtVerify the content of
result.txt.console$ cat result.txtOutput:
Hello, everyone! Greetings from Vultr! Learn cat commands with Vultr!The
>redirects the merged output to the new fileresult.txtwhile keeping the original files intact. - You can combine files using the wildcard (
*) to select multiple files and save them into a new file. For example, to combine all files in the directory that end with.txtintoresult2.txt:console$ cat *.txt > result2.txtThis would also create the file
result2.txtif it doesn’t exist. To verify the content ofresult2.txt:console$ cat result2.txtOutput:
Hello, everyone! Greetings from Vultr! Hello, everyone! Greetings from Vultr! Learn cat commands with Vultr! Learn cat commands with Vultr! - You can also insert separators or any text between files you want to combine. For example, you can insert a divider line between
example.txtandtutorial.txtas they appear in the new fileresult3.txt.console$ (cat example.txt; echo -e "\n----- DIVIDER -----\n"; cat tutorial.txt) > result3.txt
To verify the content of
result3.txt:console$ cat result3.txtOutput:
Hello, everyone! Greetings from Vultr! ----- DIVIDER ----- Learn cat commands with Vultr!
Redirect Input to Files using cat
While cat is typically used to display file contents, it can also accept input directly from the terminal and write it to a file. This makes cat a quick way to create or append content without using a text editor, especially useful on headless systems.
- Create a new file named
file1.txtby redirecting input interactively:console$ cat > file1.txtType the following content when prompted:
Web Server: Nginx OS: Ubuntu 22.04Press Ctrl + D to save and exit.
- Verify the contents of
file1.txt:console$ cat file1.txtOutput:
Web Server: Nginx OS: Ubuntu 24.04 - Append content to
file1.txtusing the>>redirection operator:console$ cat >> file1.txtWhen prompted, add the following line:
Service Provider: VultrPress Ctrl + D to finish. Then verify the updated contents:
console$ cat file1.txtOutput:
Web Server: Nginx OS: Ubuntu 24.04 Service Provider: Vultr - Write multi-line templates using a here-document (
<<EOF):console$ cat <<EOF > file2.txt ServerName: Vultr_Demo DocumentRoot: /var/www/html EOF
- View the contents of
file2.txt:console$ cat file2.txtOutput:
ServerName: Vultr_Demo DocumentRoot: /var/www/html - Inject shell variables into files using
cat. First, store the current date in a variable:console$ CURRENT_TIME=$(date)
- Append the timestamp to
file2.txtusing here-strings (<<<):console$ cat <<< "Timestamp: $CURRENT_TIME" >> file2.txt
You can use
cat <<< "text"to mimicecho, butechois more efficient for single-line output. This example usescatto stay consistent with the rest of the article. - Verify the final contents of
file2.txt:ServerName: Vultr_Demo DocumentRoot: /var/www/html Timestamp: Wed Apr 16 18:13:32 GMT 2025
Add Line Numbers to Files using cat
Line numbering is essential for debugging scripts, analyzing logs, or collaborating on code. The cat command provides two streamlined options (-n and -b) to add line numbers directly in the terminal, eliminating the need for external tools. This section demonstrates how to implement both methods and their practical applications in system administration and development workflows.
Number All Lines with -n
- Display the contents of
/etc/hostswith line numbers:console$ cat -n /etc/hostsOutput:
1 127.0.0.1 localhost 2 127.0.1.1 ubuntu-server 3 4 # IPv6 entries 5 ::1 ip6-localhost ip6-loopback - Save a timestamped copy of
/etc/os-releasewith line numbers:console$ cat -n /etc/os-release > os_snapshot_$(date +%F).txt
- Verify the new file:
1 NAME="Ubuntu" 2 VERSION="22.04.3 LTS (Jammy Jellyfish)" 3 ID=ubuntu 4 ID_LIKE=debian
Number Only Non-Empty Lines with -b
Adding line numbers by combining cat with the option -b. The -b option numbers only non-empty lines, which is useful for focusing on meaningful content in files with many blank lines, such as configuration files or sparse logs.
- Print
/etc/ssh/ssh_configwhile ignoring empty lines:console$ cat -b /etc/ssh/ssh_configOutput:
1 # This is the ssh client system-wide configuration file. See 2 # ssh_config(5) for more information. This file provides defaults for 3 # users, and the values can be changed in per-user configuration files 4 # or on the command line.
Combine Line Numbering with Pipes
You can combine cat -n or cat -b with other tools for advanced filtering and formatting.
- Search numbered lines for error messages in
syslog.console$ sudo cat -n /var/log/syslog | grep "error"
Example Output:
1452 Mar 1 14:22:01 ubuntu-server cron[1234]: ERROR: Failed to start job 1789 Mar 1 14:35:01 ubuntu-server kernel: [UFW BLOCK] IN=eth0 SRC=203.0.113.5 - Extract specific lines from
example.txtusingawk:console$ cat -n example.txt | awk 'NR==1 || NR==3 {print "Line " $1 ": " $0}'
Output:
Line 1: 1 Hello, everyone! Line 2: 2 Line 3: 3 Greetings from Vultr!
Conclusion
In this article, you explored how to use the cat command to view, merge, and manipulate file content on Linux systems. You learned how to display files, redirect input and output, append content, inject variables, and add line numbers—both interactively and through automation-friendly scripts.