cat ‐ #files - five4nets/Linux-Knowledgebase GitHub Wiki

Linux cat Command Tutorial

The cat command in Linux is a versatile utility used to display, concatenate, and create files. Its name stands for "concatenate," and it’s commonly used to read and combine file contents, redirect output, or create new files. This tutorial covers the basics of the cat command, its options, and practical examples to demonstrate its usage.

Table of Contents

What is the cat Command?

The cat command reads data from files and outputs their contents to the terminal. It can also combine multiple files or redirect output to a new file. It’s a fundamental tool in Linux for text file manipulation.

Syntax

cat [OPTION]... [FILE]...
  • OPTION: Flags to modify the behavior of the command.
  • FILE: One or more files to process. If no file is specified, cat reads from standard input.

Common Options

Option Description
-n Number all output lines, starting from 1.
-b Number non-blank output lines.
-s Suppress repeated empty output lines (squeeze multiple blank lines into one).
-E Display a $ at the end of each line.
-T Display tab characters as ^I.
-A Show all non-printing characters (combines -E and -T with other control characters).
> Redirect output to a file (overwrites).
>> Append output to a file.

Examples

1. Display a Single File

To display the contents of a file named example.txt:

cat example.txt

Output (if example.txt contains):

Hello, World!
This is a test file.

The contents are printed to the terminal.

2. Concatenate Multiple Files

To combine two files, file1.txt and file2.txt, and display their contents:

cat file1.txt file2.txt

Output (if file1.txt contains "First file" and file2.txt contains "Second file"):

First file
Second file

3. Create a New File

To create a new file using cat with input from the terminal (press Ctrl+D to save and exit):

cat > newfile.txt

Type text, e.g., "This is a new file," then press Ctrl+D. The file newfile.txt is created with the entered text.

4. Append to a File

To append the contents of file1.txt to file2.txt:

cat file1.txt >> file2.txt

This adds the contents of file1.txt to the end of file2.txt without overwriting.

5. Number Output Lines

To display example.txt with line numbers:

cat -n example.txt

Output:

     1  Hello, World!
     2  This is a test file.

6. Number Non-Blank Lines

To number only non-blank lines:

cat -b example.txt

Output (if example.txt has blank lines):

     1  Hello, World!
     2  This is a test file.

7. Suppress Repeated Blank Lines

To squeeze multiple blank lines into one:

cat -s example.txt

Output (if example.txt has multiple blank lines):

Hello, World!
This is a test file.

8. Display Non-Printing Characters

To show tabs and line endings:

cat -A example.txt

Output (tabs shown as ^I, line endings as $):

Hello, World!$
This^Iis^Ia^Itest^Ifile.$

9. Redirect Output to a File

To combine two files and save the result to a new file:

cat file1.txt file2.txt > combined.txt

This creates combined.txt with the contents of both files.

10. Read from Standard Input

To read input from the terminal and display it:

cat

Type text, e.g., "Hello from stdin," and press Ctrl+D. The input is echoed back.

11. Use cat with Pipes

To count the number of lines in a file using cat and wc:

cat example.txt | wc -l

Output:

2

References