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.
The examples below use the course setup. In ~/playground/chapter2 run cd ~/playground/chapter2 before trying the practice exercises at the bottom of the page.
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
NOTE: When writing to a file the > and >> operators are used to overwrite and append to the file respectively. The < and << are used to read from a file. This is known as redirection and will be covered in detail later in this book.
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
These exercises use practice files that are created by the course setup script. Make sure you are in the ~/playground/chapter2 directory before you start the exercises.
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:
- All commands assume you are in
~/playground/chapter2; usecd ~/playground/chapter2if needed. - If you need to reset the practice files (for example after modifying or deleting them), run
setup.shagain. - Use
lsto see all the files in the directory - Remember:
>overwrites,>>appends - When typing into a file with
cat >orcat >>, press Ctrl+D when you're done
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