
The cat command, short for "concatenate," is one of the most fundamental utilities in Unix-like operating systems. First introduced in Version 1 Unix (1971), it comes pre-installed on nearly all Linux distributions. While it is most commonly used to display file contents in the terminal, it also supports merging files, creating or appending content, and redirecting output, making it a versatile part of everyday shell scripting and file manipulation.
This article explains how to use the cat command in a variety of practical scenarios. It covers common use cases like viewing files, joining multiple files, redirecting input and output, numbering lines, and working with pipes. You'll also explore useful flags and options to make your usage of cat more efficient and adaptable across different environments.
Prerequisites
Before you begin, ensure you have:
- A Linux instance running Ubuntu or another supported distribution.
- Access to the instance via SSH.
- A non-root user with
sudoprivileges. - A text editor like
nanoto create and modify configuration files.
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.txt
Output:
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.txt
Output:
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.txt
Output:
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.txt
Output:
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.txt
Output:
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 *.txt
To read
.txtfiles from a specific directory, you can specify the path to the directory before the wildcard character.console$ cat /path/to/directory/*.txt
Replace
/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
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.txt
Verify the content of
result.txt.console$ cat result.txt
Output:
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.txt
This would also create the file
result2.txtif it doesn't exist. To verify the content ofresult2.txt:console$ cat result2.txt
Output:
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.txt
Output:
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.txt
Type 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.txt
Output:
Web Server: Nginx OS: Ubuntu 24.04Append content to
file1.txtusing the>>redirection operator:console$ cat >> file1.txt
When prompted, add the following line:
Service Provider: VultrPress Ctrl + D to finish. Then verify the updated contents:
console$ cat file1.txt
Output:
Web Server: Nginx OS: Ubuntu 24.04 Service Provider: VultrWrite 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.txt
Output:
ServerName: Vultr_Demo DocumentRoot: /var/www/htmlInject 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/hosts
Output:
1 127.0.0.1 localhost 2 127.0.1.1 ubuntu-server 3 4 # IPv6 entries 5 ::1 ip6-localhost ip6-loopbackSave 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_config
Output:
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.5Extract 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.
Whether you're managing logs, writing scripts, or debugging configuration files, cat remains a reliable and essential utility in every Linux user's toolkit. For more advanced use cases, refer to the Linux cat man page.