CIS120 Linux Fundamentals by Scott Shaper

The head, tail and wc Commands

Think of these commands as your file viewing toolkit in Linux. They help you quickly peek at files without opening them completely, which is especially useful for large files. head shows you the beginning of a file, tail shows you the end, and wc gives you quick statistics about the file's contents.

Quick Reference

Command Description Common Use
head Show beginning of file View first few lines
tail Show end of file View last few lines or monitor logs
wc Count file contents Get line/word/character counts

head Command

The head command is like looking at the first page of a book - it shows you the beginning of a file. By default, it displays the first 10 lines, but you can change this number to see more or fewer lines.

When to Use head

Use head when you want to:

Common Options

Option What It Does When to Use It
-n Show first N lines When you need a specific number of lines
-c Show first N bytes When you need to see a specific amount of data
-q Hide file names When viewing multiple files
-v Show file names When you need to know which file is which

Practical Examples

View First Lines
# Show first 10 lines (default)
head notes.txt

# Show first 5 lines
head -n 5 notes.txt

# Show first 100 bytes
head -c 100 notes.txt
View Multiple Files
# Show first 3 lines of each file
head -n 3 file1.txt file2.txt

# Show first 3 lines without file names
head -n 3 -q file1.txt file2.txt

tail Command

The tail command is like looking at the last page of a book - it shows you the end of a file. It's especially useful for checking log files or seeing recent changes.

When to Use tail

Use tail when you want to:

Common Options

Option What It Does When to Use It
-n Show last N lines When you need a specific number of lines
-c Show last N bytes When you need to see a specific amount of data
-f Follow file changes When monitoring log files
-q Hide file names When viewing multiple files
-v Show file names When you need to know which file is which

Practical Examples

View Last Lines
# Show last 10 lines (default)
tail notes.txt

# Show last 5 lines
tail -n 5 notes.txt

# Show last 100 bytes
tail -c 100 notes.txt
Monitor Log Files
# Watch a log file in real-time
tail -f server.log

# Watch multiple log files
tail -f log1.txt log2.txt

wc Command

The wc (word count) command is like a quick statistics tool for files. It counts lines, words, and characters, giving you a quick overview of a file's contents.

When to Use wc

Use wc when you want to:

Common Options

Option What It Does When to Use It
-l Count lines When you need to know how many lines
-w Count words When you need to know how many words
-c Count bytes When you need to know file size
-m Count characters When you need to know character count
-L Show longest line When checking line lengths

Practical Examples

Basic Counting
# Count lines, words, and bytes
wc notes.txt

# Count only lines
wc -l notes.txt

# Count only words
wc -w notes.txt

# Count only characters
wc -m notes.txt
Advanced Usage
# Count lines in multiple files
wc -l *.txt

# Find longest line
wc -L notes.txt

# Count words in all text files
wc -w *.txt

Tips for Success

Common Mistakes to Avoid

Best Practices