Reading and Viewing Files

Commands for viewing file contents without editing.

Command

Description

Examples

cat <file>

Display entire file

cat file.txt

head <file>

First 10 lines (default)

head file.txt
head -n 20 file.txt (20 lines)

tail <file>

Last 10 lines (default)

tail file.txt
tail -n 50 file.txt (50 lines)
tail -f log.txt (follow in real-time)

Common Patterns

cat file.txt           # Show all
head -n 20 file.txt    # First 20 lines
tail -n 20 file.txt    # Last 20 lines
tail -f log.txt        # Follow log in real-time

Tips

  • tail -f is perfect for watching log files update in real-time

  • Use head and tail for large files to avoid scrolling through everything

  • Combine with less for paginated viewing: less largefile.txt

  • Use more for simpler pagination: more largefile.txt