Linux cat Command
Linux cat Command with Examples
The Linux cat command is one of the most commonly used commands for working with text files. The name cat comes from concatenate. It is primarily used to display file contents,
combine multiple files, create files, and redirect file content in Linux and Unix-like operating systems.
cat command is a command-line utility used to read and display the contents of fileson the terminal. It can also concatenate the contents of multiple files and write the combined output to another file.
The basic syntax of the command is:
$ cat [OPTION]... [FILE]...
Why is the cat Command Used?
The cat command is useful for many everyday Linux administration and development tasks, including:
- Displaying the contents of a text file.
- Displaying multiple files.
- Combining multiple files into a single file.
- Creating a new file from the terminal.
- Appending content to an existing file.
- Displaying line numbers.
- Identifying blank lines and special characters.
1. Display the Contents of a File
The most basic use of cat is to display the contents of a file.
$ cat test.txt
Example output:
Hello World
This is a Linux file.
Learning Linux commands.
2. Display Multiple Files
You can provide multiple filenames to the cat command. Linux displays their contents sequentially.
$ cat file1.txt file2.txt
This is especially useful when you want to quickly inspect several related files without opening them separately.
3. Concatenate Multiple Files
The cat command can combine the contents of multiple files and write the result to a new file.
$ cat file1.txt file2.txt > combined.txt
In this example, the contents of file1.txt and file2.txt are combined and stored in
combined.txt.
4. Append One File to Another
The >> redirection operator can be used with cat to append content to an existing file.
$ cat file2.txt >> file1.txt
This adds the contents of file2.txt to the end of file1.txt without overwriting the
existing content.
5. Display Line Numbers
Use the -n option to display line numbers along with the contents of a file.
$ cat -n test.txt
Example output:
1 Hello World
2 This is a Linux file.
3 Learning Linux commands.
6. Number Non-Empty Lines
The -b option numbers only the non-empty lines in a file.
$ cat -b test.txt
Unlike cat -n, blank lines are not assigned line numbers.
7. Display Hidden Characters
The -v option displays non-printing characters in a visible form. This can be useful when
troubleshooting files containing unexpected characters.
$ cat -v test.txt
8. Show End of Each Line
The -E option displays a $ character at the end of each line.
$ cat -E test.txt
This is useful when checking trailing spaces or understanding the exact line structure of a text file.
9. Show Tabs
The -T option displays tab characters using a visible representation.
$ cat -T test.txt
10. Squeeze Multiple Blank Lines
The -s option compresses consecutive blank lines into a single blank line.
$ cat -s test.txt
This can make files with excessive empty lines easier to read.
11. Create a New File Using cat
The cat command can also be used to create a new file by redirecting terminal input to a file.
$ cat > newfile.txt
Enter the required content and press Ctrl + D to save the file and return to the shell.
Example:
cat > employee.txt
Name: John
Role: Software Engineer
Department: Testing
12. Append Content Using cat
To append new content to an existing file, use the >> operator.
$ cat >> employee.txt
Type the additional content and press Ctrl + D when finished.
13. Display the Contents of a System File
Linux stores important system information in files. The cat command can be used to inspect
readable system files.
$ cat /etc/hostname
This displays the hostname configured on the system.
14. Use cat with a Pipe
The output of cat can be passed to another Linux command using a pipe (|).
$ cat application.log | grep ERROR
This displays lines containing ERROR from the log file.
For simple searches, directly using grep ERROR application.log is generally preferable because it
avoids an unnecessary cat process.
15. View a File with Line Numbers and Special Characters
Several options can be combined to make file contents easier to troubleshoot.
$ cat -n -E -T test.txt
This displays line numbers, end-of-line markers, and visible tab characters.
Common cat Command Options
| Option | Description | Example |
|---|---|---|
-n |
Number all output lines | cat -n file.txt |
-b |
Number only non-empty lines | cat -b file.txt |
-E |
Show the end of each line | cat -E file.txt |
-T |
Show tab characters | cat -T file.txt |
-v |
Show non-printing characters | cat -v file.txt |
-s |
Suppress repeated blank lines | cat -s file.txt |
cat Command for Software Testers
The cat command is particularly useful for software testers and DevOps engineers working with
Linux servers. It can be used to quickly inspect configuration files, application logs, environment-related
information, deployment files, and test execution results.
For example, a tester can inspect an application log using:
$ cat application.log
To quickly identify errors, a combination with grep can be used:
$ cat application.log | grep ERROR
A tester can also inspect configuration files such as:
$ cat application.properties
cat vs less vs more
Although cat is excellent for displaying small files, other commands may be more appropriate for
large files.
| Command | Best Use |
|---|---|
cat |
Quickly display or combine file contents |
less |
Navigate through large files interactively |
more |
Paginated viewing of text files |
Important Tips When Using cat
Be careful when using output redirection with >. For example:
$ cat file1.txt > file2.txt
If file2.txt already exists, its contents are replaced. Use >> when you need to
append rather than overwrite.
Also, avoid using cat to print extremely large files directly to the terminal. Commands such as
less, head, and tail are often better suited for large files.
Frequently Asked Questions About Linux cat Command
What does cat mean in Linux?
cat stands for concatenate. The command is used to read and combine file contents.
How do I view a file in Linux?
You can use the following command:
cat filename
How do I combine two files in Linux?
Use:
cat file1.txt file2.txt > combined.txt
How do I number lines using cat?
Use the -n option:
cat -n filename
The Linux cat command is a simple but powerful command-line utility for reading,
displaying, creating, and combining text files. It is one of the fundamental Linux commands that every developer, software tester, system administrator, and DevOps engineer should know.
Once you understand cat and Linux redirection operators such as >,
>>, and |, you can perform many common file and command-line tasks efficiently.