Viewing File Contents (cat)
The cat command displays file contents. It's the simplest way to view what's inside a file.
Basic Usage
cat filename
What cat Does
cat stands for "concatenate" - it can:
- Display file contents
- Combine multiple files
- Create new files
- Number lines
cat Options
| Option | Description |
|---|---|
-n | Number all lines |
-b | Number non-blank lines only |
-s | Squeeze multiple blank lines |
-A | Show all (including hidden characters) |
-E | Show $ at end of lines |
Viewing Multiple Files
Concatenate and display multiple files:
cat file1.txt file2.txt
cat *.txt
Exercise: View a File
Display the contents of the notes.txt file:
Creating Files with cat
Using Redirection
cat > newfile.txt
Type your content here
Press Ctrl+D to save
Using Heredoc
cat > script.sh << 'EOF'
#!/bin/bash
echo "Hello World"
EOF
Combining Files
cat header.txt body.txt footer.txt > complete.txt
cat part1.txt part2.txt >> combined.txt # Append
When NOT to Use cat
For large files, use:
less- for paginghead- for first linestail- for last lines
# Avoid for large files:
cat huge_log.txt # Floods terminal
# Better alternatives:
less huge_log.txt # Page through
head huge_log.txt # First 10 lines
tail huge_log.txt # Last 10 lines
Numbered Output
cat -n file.txt # Number all lines
cat -b file.txt # Number non-blank lines
Useful Patterns
Quick File Preview
cat /etc/hostname # Check hostname
cat /etc/hosts # Check hosts file
cat ~/.bashrc # View bash config
Check File Type
cat -A file.txt # Shows hidden chars
# Useful for debugging
Key Takeaways
catdisplays file contents- Use
-nto number lines - Can combine multiple files
- For large files, use
less,head, ortail catis for quick viewing of small files

