Linux cat Command


The cat command in Linux is used for displaying, combining, and creating text files. It's a versatile tool for viewing file contents, appending text, and redirecting output to other files.

Basic Syntax

cat [options] file

Common Uses of cat

1. Viewing File Contents

Using cat with a single file displays the entire content of the file to the terminal.

Command
cat file.txt
Output
This is the content of file.txt. It can span multiple lines.

Explanation:

  • This command displays the contents of file.txt in the terminal.

2. Displaying Multiple Files

You can display multiple files consecutively by specifying them in the command. This is useful for quickly reading through multiple files without opening each one.

Command
cat file1.txt file2.txt
Output
Contents of file1.txt --------------------- This is file1. Contents of file2.txt --------------------- This is file2.

Explanation:

  • cat file1.txt file2.txt outputs the contents of both file1.txt and file2.txt, showing them one after the other.

3. Creating a New File

You can use cat with redirection (>) to create a new file and add content to it.

Command
cat > newfile.txt
Input (type and press Ctrl+D to save):
This is a new file. It contains some text.
Output
This is a new file. It contains some text.

Explanation:

  • cat > newfile.txt lets you create newfile.txt. Whatever you type in the terminal is saved to the file when you press Ctrl+D.

4. Appending Content to an Existing File

Using >> allows appending additional content to the end of an existing file without overwriting it.

Command
cat >> file.txt
Input (type and press Ctrl+D to save):
This is additional content.
Output in file.txt
This is the content of file.txt. It can span multiple lines. This is additional content.

Explanation:

  • cat >> file.txt opens file.txt for appending. The additional content is added to the end when you press Ctrl+D.

5. Displaying Line Numbers

The -n option displays line numbers for each line of the file.

Command
cat -n file.txt
Output
1 This is the content of file.txt. 2 It can span multiple lines.

Explanation:

  • cat -n file.txt adds line numbers to each line in file.txt, making it useful for reviewing line-by-line content.

6. Merging Multiple Files into One

You can combine multiple files into a single file by redirecting the output of multiple files to a new one.

Command
cat file1.txt file2.txt > merged.txt
Output in merged.txt
This is file1. This is file2.

Explanation:

  • cat file1.txt file2.txt > merged.txt combines file1.txt and file2.txt and saves the result to merged.txt.

Summary of Common cat Commands

CommandDescription
cat file.txtDisplays the content of file.txt.
cat file1.txt file2.txtDisplays the contents of multiple files.
cat > newfile.txtCreates a new file and lets you type its content.
cat >> file.txtAppends new content to the end of file.txt.
cat -n file.txtDisplays content with line numbers.
cat file1.txt file2.txt > merged.txtCombines multiple files into one.

The cat command is quick and efficient for managing text file content, allowing users to display, create, append, and merge files easily.