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
First, create practice files by running these commands:
Create a file with multiple lines for head and tail examples:
cat > notes.txt << 'EOF'
Line 1: Introduction
Line 2: Getting Started
Line 3: Basic Concepts
Line 4: Advanced Topics
Line 5: Examples
Line 6: Practice Exercises
Line 7: Common Mistakes
Line 8: Best Practices
Line 9: Tips and Tricks
Line 10: Summary
Line 11: Additional Resources
Line 12: Conclusion
Line 13: References
Line 14: Appendix A
Line 15: Appendix B
EOF
Create two additional files for multiple file examples:
cat > file1.txt << 'EOF'
First file line 1
First file line 2
First file line 3
First file line 4
First file line 5
EOF
cat > file2.txt << 'EOF'
Second file line 1
Second file line 2
Second file line 3
Second file line 4
Second file line 5
EOF
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:
- 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
Create log files for tail examples:
Create a server log file:
cat > server.log << 'EOF'
2024-01-01 10:00:00 Server started
2024-01-01 10:05:23 User connected
2024-01-01 10:10:45 Request processed
2024-01-01 10:15:12 Error occurred
2024-01-01 10:20:30 User disconnected
2024-01-01 10:25:55 New connection
2024-01-01 10:30:18 Request completed
2024-01-01 10:35:42 Server status OK
2024-01-01 10:40:15 Warning logged
2024-01-01 10:45:00 Server shutdown
EOF
Create additional log files for multiple file examples:
cat > log1.txt << 'EOF'
Log 1 entry 1
Log 1 entry 2
Log 1 entry 3
Log 1 entry 4
Log 1 entry 5
EOF
cat > log2.txt << 'EOF'
Log 2 entry 1
Log 2 entry 2
Log 2 entry 3
Log 2 entry 4
Log 2 entry 5
EOF
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:
- 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