Introduction
The cat
(concatenate) command in Linux displays file contents. It reads one or multiple files and prints their content to the terminal. cat
is used to view file contents, combine files, and create new files.
This tutorial explains how to use the Linux cat
command with practical examples.
Prerequisites
- A system running Linux (this tutorial uses Ubuntu 22.04).
- Access to the terminal.
cat Command Syntax
The cat
command syntax is:
cat [options] [file_name]
Without a file name or options, the cat
command repeats any provided input. Adding options modifies how the command works, and supplying a file name specifies the file to work with.
cat Command Options
The cat
command has several options that modify its output. Commonly used options are shown in the table below.
Option | Description |
---|---|
-A | Shows all characters, including non-printing characters and line endings. |
-b | Creates a numbered list with non-blank lines. |
-e | Shows non-printing characters and ends lines with $ . |
-E | Displays a $ at the end of each line. |
-n | Creates a numbered list with all lines, including blank lines. |
-s | Displays non-printing characters, except for tabs and end-of-line characters. |
-T | Shows tab characters as ^I . |
-v | Displays non-printing characters, except for tabs and end-of-line characters. |
cat Command Examples
Below are cat
commands and examples of how to use them. To try the commands, create several sample files and test the cat
commands.
Create a New File
Use the cat
command to create new files and add content to them. Create test1.txt and test2.txt files to use as sample files and test other command examples.
To create the files, do the following:
1. Open a terminal window and create the first file:
cat >test1.txt
2. The cursor moves to a new line where you add text. Type a sentence, such as:
This is test file #1.
3. To exit the prompt and write the changes to the file, press Ctrl+d.
4. Repeat the process to create test2.txt. Run the following:
cat >test2.txt
5. Add sample text to the file:
This is test file #2.
6. Press Ctrl+d to save and exit.
Display Contents of a Single File
To display the test1.txt contents using the cat
command, run:
cat test1.txt
The output displays the content of file test1.txt.
Display Contents of Multiple Files
To show the contents of both files, run:
cat test1.txt test2.txt
The command shows file contents in sequential order.
Redirect Contents of a Single File
Instead of displaying the file contents on the screen, redirect the file contents to another file. For example:
cat test1.txt > test3.txt
The command has no output. If the destination filename doesn't exist, the command creates it. Run cat
on test3.txt to see the file's contents:
cat test3.txt
If a file with the same name exists, its contents are overwritten. For example, the test2.txt contents overwrite the existing test3.txt contents:
cat test2.txt > test3.txt
The command has no output. Run the following to verify:
cat test3.txt
The original contents are overwritten with test2.txt contents.
Redirect Contents of Multiple Files
Redirect the contents of multiple files into a single file. Run the following:
cat test1.txt test2.txt > test3.txt
The command has no output. Display the test3.txt contents with:
cat test3.txt
The output shows the contents of both files appended to the test3.txt file.
Display the Contents in Reverse Order
The cat
command can display the file content in reverse order (by lines). To do this, use tac
(cat
in reverse):
tac test3.txt
The file contents print in reverse order, starting from the last line.
Append File Contents to Another File
Use the cat
command with >>
to append file contents to the end of another file. For example:
1. Create a new file called test4.txt:
cat > test4.txt
The command has no output.
2. Add the following line to the file:
This is test file #4
3. Save the file with Ctrl+d.
4. Add test1.txt contents to test4.txt with:
cat test1.txt >> test4.txt
5. Run the following to see test4.txt contents:
cat test4.txt
The terminal shows test1.txt contents appended to the test4.txt file.
Note: To remove the sample files created during this tutorial, check out how to remove files and directories using the Linux command line.
Append Text to Existing File
Use cat
with >>
to append text to an existing file. Follow these steps:
1. Run the following to open the file for editing:
cat >> test1.txt
2. Add a new line to the file:
This is the second line in test file #1.
3. Hold Ctrl+d to save and exit:
4. Check the test1.txt file contents with:
cat test1.txt
The process appended a new line to the file.
Combine Operations
The cat
command functions can be combined to create complex operations. For example, to combine the output of two files and store the result in a new file, run:
cat test1.txt test2.txt > test5.txt
cat test5.txt
Note: Once you have created multiple files, you can group them in a single directory. See how to use the mkdir command to make or create a Linux directory.
More and Less Options (Manage Large Files)
If you use cat
on a large file, you'll end up with a huge data string that's hard to read. Break the file into pages by piping the more
command. For example, for a large file called test6.txt, use:
cat test6.txt | more
The command displays a single file page. Press any key to scroll to the next page.
To scroll forward and backward through the display, pipe the less command instead:
cat test6.txt | less
Note: Learn how to use the Linux split command to manage text files with many lines easily.
Show Line Numbering
The cat
command can add line numbers to the output. To enable line numbering, add the -n
option:
cat -n test1.txt
The output shows line numbers for each new line.
Show the End of the Line
Instruct cat
to highlight the end of each line and spaces between lines with a dollar sign ($). To do so, use the command:
cat -e test1.txt
The option labels line endings and empty lines.
Show Tab-Separated Lines
Use the -t
option to display the file contents and label tab spaces within the text. To show tab-separated lines for a sample run:
cat -t test1.txt
The tab space within the text is represented by ^I
(for indent).
Number Non-Blank Lines
To create a numbered list of non-blank lines, use the –b
option. For example, if the test1.txt includes empty lines:
cat test1.txt
Number non-blank lines with:
cat -b test1.txt
The option skips empty lines and continues numbering lines with text.
Remove Blank Lines
To remove multiple blank lines in the text, use the -s
option. For example, add three blank lines in test1.txt:
To merge those empty lines into one line, run:
cat -s test1.txt
The option suppresses the repeated empty lines into one.
List All cat Commands
To view all cat
command options, use the --help
option:
cat --help
Note: Learn how to concatenate strings in Bash.
Conclusion
This article showed practical examples of how to use the cat
command in Linux. Next, learn how to master other Linux Commands.