Introduction

Hello! In this article, we’ll learn about the cat command.

cat is a command for displaying file contents, and it’s one of the absolute must-know basic commands in Linux. The name comes from “concatenate,” but most of the time we just use it to view files.

What is the cat Command?

cat is a command that displays file contents to standard output (the screen) or concatenates multiple files together.

When you want to quickly check what’s inside a text file or combine multiple files into one, this is what you use. Super simple, but used incredibly frequently.

Basic Syntax

1
cat [options] [filename...]

Main Options

Option Description
-n Number all output lines
-b Number non-empty output lines only
-s Squeeze multiple adjacent blank lines into one
-E Display $ at end of each line
-T Display TAB characters as ^I
-A Equivalent to -vET (show all special characters)
-v Make non-printing characters visible

Usage Examples

Example 1: Display File Contents

1
cat sample.txt

Output:

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

This is the most basic usage. The file contents are displayed on the screen.

Example 2: Display Multiple Files

1
cat file1.txt file2.txt

Output:

1
2
Contents of file1
Contents of file2

When you specify multiple files, they’re concatenated and displayed in order.

Example 3: Display with Line Numbers

1
cat -n sample.txt

Output:

1
2
3
4
     1	Hello, World!
     2	This is a sample file.
     3
     4	End of file.

The -n option adds numbers to all lines. Note that blank lines also get numbers.

Example 4: Number Non-Empty Lines Only

1
cat -b sample.txt

Output:

1
2
3
4
     1	Hello, World!
     2	This is a sample file.

     3	End of file.

The -b option doesn’t number blank lines. This is often more readable.

Example 5: Create a New File

1
cat > newfile.txt

After running this, it waits for input. Type whatever you want. Press Ctrl + D to save and exit.

1
2
3
Hello there
This is a new file
[Press Ctrl + D]

Handy for creating simple files.

Example 6: Append to a File

1
cat >> existing.txt

Using >> appends to an existing file. Also press Ctrl + D to save.

Example 7: Concatenate and Save Files

1
cat file1.txt file2.txt > combined.txt

You can combine multiple files into one and save to a new file. Useful when you want to merge log files.

Example 8: Visualize Line Endings

1
cat -E sample.txt

Output:

1
2
3
4
Hello, World!$
This is a sample file.$
$
End of file.$

The -E option displays $ at the end of each line. Useful for checking line endings or trailing spaces.

Example 9: Visualize Tabs

1
cat -T sample.txt

Output:

1
2
Name:^IAge
Taro^I25

The -T option displays tabs as ^I. Use this when you can’t tell spaces from tabs.

Example 10: Combine with Pipes

1
cat /var/log/syslog | grep error

Often used in combination with other commands. This example displays only lines containing “error” from the log file.

Tips & Notes

  • Be careful with large files: cat displays all content at once, so huge files will scroll like crazy. In those cases, use less or more instead

    1
    
    less bigfile.txt  # Better choice
    
  • Read from standard input: If you don’t specify a filename, it reads from standard input

    1
    2
    
    echo "test" | cat
    # Output: test
    
  • Mix files and standard input with dash: - represents standard input

    1
    2
    
    cat file1.txt - file2.txt
    # Displays file1, then stdin, then file2
    
  • Don’t display binary files: Using cat on binary files can mess up your terminal display. If you accidentally do this, use the reset command to fix it

  • Redirection caution: Reading and writing the same file will erase its contents

    1
    
    cat file.txt > file.txt  # Don't do this! File will be emptied
    

Practical Usage

Check Log Files

1
cat /var/log/syslog | tail -n 20

When you want to see just the last 20 lines of a log.

Check Configuration Files

1
cat ~/.bashrc

Quickly check your bash configuration file.

Create File with Here Document

1
2
3
4
5
cat > config.txt << EOF
server=localhost
port=8080
debug=true
EOF

You can create multi-line files in one go. This is a common technique in scripts.

Combine Multiple Logs

1
cat access.log.1 access.log.2 access.log.3 > all_access.log

Useful for merging split log files into one.

Code Review with Line Numbers

1
cat -n script.sh

When discussing specific lines of code, line numbers are helpful.

Summary

In this article, we learned about the cat command.

Key Points:

  • cat is a basic command for displaying file contents
  • Can concatenate multiple files (that’s where the name comes from!)
  • -n for line numbers, -E to visualize line endings
  • Combine with redirection to create/concatenate files
  • For large files, less or more are better choices

Simple but super frequently used command. When you want to quickly peek at a file, just cat it.

Once you learn to use cat together with less, head, and tail, you’ll work much more efficiently.

Keep learning Linux commands!