Linux cat Command (With Examples)

June 3, 2024


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.

Linux cat Command (With Examples)


  • 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.

-AShows all characters, including non-printing characters and line endings.
-bCreates a numbered list with non-blank lines.
-eShows non-printing characters and ends lines with $.
-EDisplays a $ at the end of each line.
-nCreates a numbered list with all lines, including blank lines.
-sDisplays non-printing characters, except for tabs and end-of-line characters.
-TShows tab characters as ^I.
-vDisplays 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
cat >test1.txt terminal output

2. The cursor moves to a new line where you add text. Type a sentence, such as:

This is test file #1.
adding text to the file

3. To exit the prompt and write the changes to the file, press Ctrl+d.

saving the file terminal output

4. Repeat the process to create test2.txt. Run the following:

cat >test2.txt
cat >test2.tzt

5. Add sample text to the file:

This is test file #2.
Adding text to the second file terminal output

6. Press Ctrl+d to save and exit.

Saving second file terminal output

Display Contents of a Single File

To display the test1.txt contents using the cat command, run:

cat test1.txt
cat test1.txt terminal output

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
cat test1.txt test2.txt terminal output

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
cat test3.txt terminal output

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
terminal output for 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
cat test1.txt test2.txt > test3.txt terminal output

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
tact test3.txt terminal output

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.

making test4.txt terminal output

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
cat test4.txt terminal output

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:

Appending text to text1.txt terminal output

4. Check the test1.txt file contents with:

cat test1.txt
cat test1.txt terminal output

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
cat test1.txt test2.txt > test6.txt terminal output

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
cat test6.txt | more terminal output

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
cat test6.txt | less terminal output

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
cat -n test1.txt terminal output

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
cat -e test1.txt terminal output

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
cat -t test1.txt terminal output

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
cat text1.txt terminal output

Number non-blank lines with:

cat -b test1.txt
cat -b test1.txt terminal output

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:

cat test1.txt empty lines terminal output

To merge those empty lines into one line, run:

cat -s test1.txt
cat -s test1.txt terminal output

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
cat --help terminal output

Note: Learn how to concatenate strings in Bash.


This article showed practical examples of how to use the cat command in Linux. Next, learn how to master other Linux Commands.

Was this article helpful?
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the...
Read more
How to Extract or Unzip tar.gz Files from Linux Command Line
February 12, 2024

This article shows which commands best to use when compressing and decompressing files from the command line...
Read more
How to Create a File in Linux Using Terminal/Command Line
July 11, 2024

Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques. In...
Read more
22 Best Linux Text Editors for Programming & Coding
September 3, 2019

A text editor is an application that lets you type text. All Linux distributions come with built-in editors...
Read more