
The cat Command
Think of the cat
command as your Swiss Army knife for working with text files in Linux. It's short for "concatenate" (which means to join things together), but it can do much more than just combine files. You can use it to view file contents, create new files, and even add text to existing files.
Why Learn the cat Command?
The cat
command is essential because:
- It's one of the most basic and frequently used commands
- It helps you quickly view file contents
- It's useful for creating and modifying text files
- It's often used in scripts and automation
Basic Syntax
cat [options] [file1] [file2] ...
Common Options
Option | What It Does | When to Use It |
---|---|---|
-n |
Numbers all lines | When you need to reference specific lines |
-v |
Shows non-printing characters | When you need to see special characters |
-b |
Numbers only non-blank lines | When you want to ignore empty lines |
-s |
Removes extra blank lines | When your file has too many empty lines |
-E |
Shows $ at the end of each line | When you need to see line endings |
-T |
Shows tabs as ^I | When you need to see tab characters |
-A |
Shows all special characters | When debugging file formatting |
Practical Examples
Viewing a File
The most basic use of cat
is to display a file's contents:
cat notes.txt
This shows everything in notes.txt
on your screen.
Viewing Multiple Files
You can view several files at once:
cat file1.txt file2.txt
This shows the contents of both files, one after the other.
Creating a New File
To create a new file and type its contents:
cat > newfile.txt
Type your text here
Press Ctrl+D when done
This creates newfile.txt
with whatever you type. Press Ctrl+D to save and exit.
Adding to a File
To add text to the end of an existing file:
cat >> existing.txt
This text will be added to the end
Press Ctrl+D when done
This adds your new text to the end of existing.txt
.
Viewing with Line Numbers
To see a file with line numbers:
cat -n notes.txt
This shows each line with its number, making it easier to reference specific parts.
Viewing Special Characters
To see all special characters (like tabs and line endings):
cat -A notes.txt
This shows:
- $ at the end of each line
- ^I for tab characters
- Other special characters
Viewing Non-Printing Characters
Sometimes text files contain characters that you can't see normally, like special formatting or control codes. The -v
option makes these invisible characters visible:
cat -v notes.txt
For example, if your file contains:
- A tab character, it will show as
^I
- A Windows-style line ending, it will show as
^M
- Special characters like © or é, they will show as
M-
followed by a code
Note: The -A
option is actually more comprehensive than -v
. It's equivalent to using -vET
(showing non-printing characters, line endings with $, and tabs as ^I). Use -A
when you want to see everything, or -v
when you only need to see non-printing characters without the line ending $ symbols.
Tips for Success
- Use -n for large files: Line numbers help you keep track of where you are
- Be careful with >: It overwrites existing files
- Use >> to add safely: It adds to the end without deleting
- Press Ctrl+D: This is how you finish typing when creating or adding to files
Common Mistakes to Avoid
- Using
>
instead of>>
when you want to add to a file - Forgetting to press Ctrl+D when creating or adding to files
- Not checking file contents before overwriting
- Using cat on very large files (use less instead)
Best Practices
- Use line numbers (-n) when working with code or configuration files
- Use -A when debugging formatting issues
- Always double-check before using > to overwrite files
- For large files, consider using less or more instead