head
Overview
The head
command outputs the first part of files. By default, it prints the first 10 lines of each file to standard output.
Syntax
head [options] [file...]
Common Options
Option | Description |
---|---|
-n num |
Print first num lines |
-c num |
Print first num bytes |
-q |
Never print headers |
-v |
Always print headers |
--bytes=[-]num |
Print first num bytes |
--lines=[-]num |
Print first num lines |
-z |
Line delimiter is NUL |
--help |
Display help message |
--version |
Output version information |
Key Use Cases
- View file beginning
- Check file headers
- Monitor log files
- Quick file inspection
- Data sampling
Examples with Explanations
Example 1: Default Usage
head file.txt
Show first 10 lines
Example 2: Specific Lines
head -n 5 file.txt
Show first 5 lines
Example 3: Multiple Files
head -n 3 file1 file2
Show first 3 lines of each file
Understanding Output
- Default: 10 lines
- With multiple files:
- ==> filename <== headers
- Error messages for:
- File not found
- Permission denied
- Invalid number argument
Common Usage Patterns
View file start:
head -n 20 file
Check byte count:
head -c 1000 file
Monitor new log entries:
head -f logfile
Performance Analysis
- Fast operation
- Memory efficient
- Handles large files well
- Stream processing
- Multiple file overhead
Additional Resources
Use Cases
- Log file inspection
- File format verification
- Quick content preview
- Data sampling
- Script debugging