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.
You can also create mutli-line files with the << 'EOF' syntax. This is a here document. It is a way to create a file with multiple lines of text.
cat > newfile.txt << 'EOF'
This is the first line
This is the second line
This is the third line
EOF
This creates newfile.txt with the three lines. NOTE: When using the << 'EOF' syntax, you do not need to use the Ctrl+D command to save and exit. The EOF is the end of the file just press the enter key to save and exit.
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.
Practice Exercises
Try these exercises to practice using the cat command. Create a practice directory first with mkdir practice_cat and then cd practice_cat to work through these exercises.
First, create these practice files by running these commands
NOTE: The << 'EOF' is used to create a here document. It is a way to create a file with multiple lines of text.
Create a simple two-line file:
cat > hello.txt << 'EOF'
Hello, World!
This is my first file.
EOF
Create a single-line file:
cat > file1.txt << 'EOF'
First file
EOF
Create another single-line file:
cat > file2.txt << 'EOF'
Second file
EOF
Create a file with blank lines between text:
cat > mixed.txt << 'EOF'
Line one
Line three
Line five
EOF
Create a file with a tab character between words:
cat > special.txt << 'EOF'
Word1 Word2
EOF
Create a file with multiple consecutive blank lines:
cat > spaces.txt << 'EOF'
First line
Second line
Third line
EOF
Create a header file for the report exercise:
cat > header.txt << 'EOF'
Header
Title: My Report
EOF
Create a body file for the report exercise:
cat > body.txt << 'EOF'
Body content here
More details
EOF
Create a footer file for the report exercise:
cat > footer.txt << 'EOF'
Footer
End of report
EOF
Create a file with mixed formatting (tabs and spaces) for debugging:
cat > debug.txt << 'EOF'
Line with tab here
Line with spaces
Normal line
EOF
Now practice these cat commands:
- Basic file viewing
cat hello.txt- View the contents of hello.txtcat file1.txt- View file1.txtcat file2.txt- View file2.txt
- View files with line numbers
cat -n hello.txt- View hello.txt with line numberscat -n file1.txt- View file1.txt with line numbers- What line number is "Hello, World!" on?
- View multiple files together
cat file1.txt file2.txt- View both files at oncecat hello.txt file1.txt file2.txt- View all three files- Notice how the contents are displayed one after the other
- Practice with line numbering options
cat -n mixed.txt- See all lines numbered (including blank lines)cat -b mixed.txt- See only non-blank lines numbered- Compare the output of both commands. What's the difference?
- View special characters
cat special.txt- View it normallycat -A special.txt- See the tab character displayed as^Icat -T special.txt- See only tabs as^Icat -E special.txt- See only line endings with$- Notice the differences between these options
- Combine files into a new file
cat file1.txt file2.txt > combined.txt- Combine the two files into a new filecat combined.txt- Verify the contentscat -n combined.txt- See the combined file with line numbers
- Practice with the -s option
cat spaces.txt- See it normally with multiple blank linescat -s spaces.txt- See how multiple blank lines are reduced to single blank lines- Compare the output - notice the difference?
- Debug formatting issues
cat debug.txt- View the file normallycat -A debug.txt- View all special characters- Identify where tabs (
^I) and line endings ($) appear cat -v debug.txt- View non-printing characters- Compare
-Aand-voutputs
- Add content to an existing file
cat >> hello.txt- Then type "This line was added later." and press Enter, then Ctrl+Dcat hello.txt- Verify the new line was added to the endcat -n hello.txt- View with line numbers to see the new line
- Challenge: Create a formatted report
cat header.txt body.txt footer.txt > report.txt- Combine all three into report.txtcat report.txt- View the complete reportcat -n report.txt- View with line numberscat >> report.txt- Then type "Generated: $(date)" and press Enter, then Ctrl+D (or just type a timestamp manually)cat report.txt- Verify the timestamp was added
Tips for completing these exercises:
- If you make a mistake, you can delete files with
rm filename.txtand recreate them - Use
lsto see all the files you've created - Remember:
>overwrites,>>appends - Always press Ctrl+D when you're done typing into a file
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