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:
- Quickly check the beginning of a file
- See file headers or metadata
- Preview large files without loading them completely
- Check the format of a file
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
The examples below use the course setup. Work in ~/playground/chapter3, where the setup has created headtail1.txt, headtail2.txt, and headtail3.txt. Use cd ~/playground/chapter3 before trying the commands.
View First Lines
# Show first 10 lines (default)
head headtail1.txt
# Show first 5 lines
head -n 5 headtail1.txt
# Show first 100 bytes
head -c 100 headtail1.txt
View Multiple Files
# Show first 3 lines of each file
head -n 3 headtail2.txt headtail3.txt
# Show first 3 lines without file names
head -n 3 -q headtail2.txt headtail3.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:
- Check the end of a file
- Monitor log files in real-time
- See recent changes to a file
- Check the last few lines of output
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
The examples below use the course setup. Work in ~/playground/chapter3, where the setup has created server1.log, server2.log, and server3.log. Use cd ~/playground/chapter3 before trying the commands.
View Last Lines
# Show last 10 lines (default)
tail headtail1.txt
# Show last 5 lines
tail -n 5 headtail1.txt
# Show last 100 bytes
tail -c 100 headtail1.txt
Monitor Log Files
# Watch a log file in real-time
tail -f server1.log
# Watch multiple log files
tail -f server2.log server3.log
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:
- Count lines in a file
- Count words in a document
- Check file size in characters/bytes
- Get quick statistics about text
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
The wc examples use the files created above (notes.txt, file1.txt, file2.txt, log1.txt, log2.txt).
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
- Use -n for specific numbers: Always specify how many lines you want
- Try -f for logs: Great for watching log files in real-time
- Combine with other commands: Use pipes (|) to process output
- Use -q for clean output: When viewing multiple files
Common Mistakes to Avoid
- Forgetting to specify number of lines with -n
- Using -f on non-log files
- Not using quotes for filenames with spaces
- Using wc without options when you need specific counts
Best Practices
- Use head/tail to preview large files
- Use -f to monitor important log files
- Use wc to check file sizes before processing
- Combine these commands with grep for powerful text processing