Viewing Files¶
Commands for reading and examining file contents without editing.
cat - Concatenate and Print¶
The simplest way to view file contents:
cat file.txt # Print entire file
cat file1.txt file2.txt # Concatenate multiple files
cat -n file.txt # Number all lines
cat -b file.txt # Number non-empty lines
cat -s file.txt # Squeeze blank lines
cat -A file.txt # Show all (tabs, line endings)
Showing Special Characters¶
Shows:
^Ifor tabs$at end of lines^Mfor Windows carriage returns
Useful for debugging whitespace issues.
Creating Files with cat¶
less - Pager for Large Files¶
For files too large to fit on screen:
Navigation¶
| Key | Action |
|---|---|
Space / f | Forward one page |
b | Back one page |
d | Forward half page |
u | Back half page |
g | Go to beginning |
G | Go to end |
q | Quit |
Searching¶
| Key | Action |
|---|---|
/pattern | Search forward |
?pattern | Search backward |
n | Next match |
N | Previous match |
&pattern | Show only matching lines |
Useful Options¶
less -N file.txt # Show line numbers
less -S file.txt # Don't wrap long lines
less -i file.txt # Case-insensitive search
less +F file.txt # Follow mode (like tail -f)
less +/pattern file.txt # Start at first match
Less vs More¶
more is older and less capable. Always prefer less:
# less has:
# - Backward navigation
# - Better search
# - No screen clear on exit (with -X)
less -X file.txt
head - View Beginning¶
head file.txt # First 10 lines (default)
head -n 20 file.txt # First 20 lines
head -n -5 file.txt # All but last 5 lines
head -c 100 file.txt # First 100 bytes
Multiple files:
tail - View End¶
tail file.txt # Last 10 lines (default)
tail -n 20 file.txt # Last 20 lines
tail -n +5 file.txt # Starting from line 5
tail -c 100 file.txt # Last 100 bytes
Follow Mode¶
Watch a file for changes (essential for logs):
tail -f /var/log/system.log # Follow file
tail -F /var/log/system.log # Follow, handles rotation
tail -f -n 50 logfile # Last 50 lines, then follow
Exit follow mode with Ctrl+C.
Multiple Files¶
Combining head and tail¶
View lines 10-20:
Or with sed:
wc - Word Count¶
wc file.txt # lines, words, bytes
wc -l file.txt # Lines only
wc -w file.txt # Words only
wc -c file.txt # Bytes only
wc -m file.txt # Characters only
Example:
file - Identify File Type¶
Determine what a file contains:
Useful for:
- Files without extensions
- Verifying file types
- Detecting encoding issues
MIME Type¶
Modern Alternative: bat¶
bat is a cat replacement with syntax highlighting:
bat file.py # Syntax highlighting
bat -n file.txt # Line numbers only (no header)
bat -p file.txt # Plain (no decorations)
bat -A file.txt # Show non-printable characters
bat --diff file.txt # Show git changes
Features:
- Automatic syntax detection
- Git integration
- Line highlighting
- Paging built-in
See Modern Replacements.
Viewing Specific Parts¶
Extract Lines by Number¶
With sed:
sed -n '5p' file.txt # Line 5 only
sed -n '5,10p' file.txt # Lines 5-10
sed -n '5p;10p;15p' file.txt # Lines 5, 10, and 15
With awk:
Extract Columns¶
Viewing Binary Files¶
hexdump / xxd¶
hexdump -C file.bin | head # Hex + ASCII
xxd file.bin | head # Alternative format
xxd -b file.bin | head # Binary format
strings¶
Extract readable text from binary:
Comparing Files¶
diff¶
diff file1.txt file2.txt # Show differences
diff -u file1.txt file2.txt # Unified format (patch style)
diff -y file1.txt file2.txt # Side by side
diff -q file1.txt file2.txt # Just report if different
comm¶
Compare sorted files:
Output columns:
- Lines only in file1
- Lines only in file2
- Lines in both
Practical Examples¶
Check Log Errors¶
Quick File Preview¶
Count Lines of Code¶
View CSV Header¶
Check File Encoding¶
Try It¶
-
Create a test file:
-
Practice viewing:
-
Use less:
-
Count and identify:
-
Clean up:
Summary¶
| Command | Purpose |
|---|---|
cat | Print entire file |
less | Page through file |
head | View beginning |
tail | View end |
tail -f | Follow file updates |
wc | Count lines/words/bytes |
file | Identify file type |
diff | Compare files |
bat | Syntax-highlighted viewing |