Daily Linux: The head Command – A Practical Tool for Quickly Viewing the Beginning of Files

1. Command Introduction and Principles

The head command is a simple and efficient command-line tool used to display the beginning portion of files. Its design philosophy is “quick preview,” making it particularly suitable for viewing the start of large files.

Working Principles

  • Sequential Reading: Reads lines from the beginning of the file until the specified number of lines or bytes is reached.

  • Early Termination: Stops reading immediately upon reaching the specified condition, without processing the remaining part of the file.

  • Stream Processing: Supports standard input and can handle piped data.

  • Efficient Performance: Only reads the necessary parts for large files, consuming minimal resources.

Core Features

  • Displays the first 10 lines by default.

  • Supports displaying by line count and byte count.

  • Can handle multiple files.

  • Complementary to the tail command.

2. Basic Syntax

head [options] [file...]

Common Options

-n, --lines=[-]NUM    # Display the first NUM lines (negative value means all lines except the last NUM lines) -c, --bytes=[-]NUM    # Display the first NUM bytes (negative value means all bytes except the last NUM bytes) -q, --quiet, --silent # Do not display file name header (when multiple files) -v, --verbose         # Always display file name header -z, --zero-terminated # Use NUL character as line separator (instead of newline)

3. Classic Use Cases

3.1 View the Beginning of a File

# View the first 10 lines of a file (default) head filename.txt # View the first 20 lines head -n 20 logfile.log # View the first 100 bytes head -c 100 data.bin

3.2 View Multiple Files

# View the first 10 lines of multiple files (show file names) head file1.txt file2.txt file3.txt # Silent mode to view multiple files (do not show file names) head -q *.log

3.3 View the Beginning of Command Output

# View the first 10 processes ps aux | head # View the first 5 directory listings ls -la | head -n 5 # View the first 3 system users cat /etc/passwd | head -n 3

4. Combining with Other Tools

4.1 Combine with tail

# View lines 6-15 of a file (first get the first 15 lines, then get the last 10 lines) head -n 15 filename.txt | tail -n 10 # View a middle portion of a file head -n 50 large_file.csv | tail -n 20

4.2 Combine with grep

# Search and view the first few results grep -r "error" /var/log/ | head -n 10 # View the first few lines containing a specific pattern grep -i -A2 -B2 "warning" app.log | head -n 20 # In the output, grep will insert -- separators between different matching blocks (default behavior), which will also count towards the line number, affecting head's output.

4.3 Combine with sort/uniq

# Sort and take the first few sort -nr data.txt | head -n 5 # Count duplicates and take the first few sort file.txt | uniq -c | sort -nr | head -n 10

4.4 Combine with awk/sed

# Process data and take the first few lines awk '{print $1, $3}' data.csv | head -n 15 # Extract a specific range of lines sed -n '1,100p' large_file.txt | head -n 50

5. Advanced Use Cases

5.1 Exclude File Tail Content

# View all lines except the last 5 head -n -5 filename.txt # View all content except the last 100 bytes head -c -100 data.txt

5.2 Monitor Changes at the File Head

# Combine with watch to monitor the file head in real-time watch -n 1 'head -n 20 logfile.log' # Monitor the head of newly created files inotifywait -m -e create /var/log/ | while read dir action filename; do echo "New file: $dir$filename" head -n 5 "$dir$filename" done

5.3 Data Processing Pipeline

# Complex data processing pipeline cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 10 # CSV file processing csvtool head 100 large.csv | head -n 20 | csvlook

5.4 Binary File Inspection

# View the header of a binary file head -c 256 binary_file | hexdump -C # Check file type head -c 20 unknown_file | file -

6. Common Errors and Avoidance Strategies

Error 1: Incorrect Format of Line Count Parameter

# Error: Incorrect parameter format head -n20 file.txt    # Missing space (some versions do not support) head -n 20k file.txt  # Invalid suffix # Correct usage head -n 20 file.txt head -c 20K file.txt  # Supports K, M, G, etc. suffixes (byte mode)

Error 2: Processing Non-existent Files

# Error: File does not exist head nonexistent_file.txt # Solution: Check file existence first [ -f file.txt ] && head file.txt # Or use error handling head file.txt 2>/dev/null || echo "File does not exist"

Error 3: Binary File Displays Garbage

# Binary file displays garbage in terminal head binary_file # Solution: View in hexadecimal head -c 100 binary_file | od -x # Or use a dedicated tool hexdump -C binary_file | head -n 10

7. Practical Tips and Examples

7.1 Quick File Check

# Check file format and structure head -n 5 data.csv # View the beginning of a configuration file head -n 20 /etc/nginx/nginx.conf # Check the header of a script file head -n 10 script.sh

7.2 System Monitoring and Debugging

# View recently logged in users last | head -n 10 # View system load history sar | head -n 15 # View disk space usage df -h | head -n 5

7.3 Data Analysis and Processing

# View sample of data file head -n 100 dataset.csv > sample.csv # Check number of data columns head -n 1 data.csv | tr ',' '\n' | wc -l # Preview JSON data head -c 500 api_response.json | python -m json.tool

7.4 Development Debugging

# View the beginning of compilation logs make 2>&1 | head -n 30 # Check API response headers curl -I https://api.example.com | head -n 10 # View the beginning of test output pytest -v | head -n 20

8. Conclusion

Core Advantages

  1. Efficient and Fast: Only reads the necessary parts, performing excellently with large files.

  2. Simple and Intuitive: Simple syntax, easy to understand and use.

  3. Resource-Friendly: Low memory usage, suitable for resource-constrained environments.

  4. Pipeline-Friendly: Perfectly integrates into the Unix pipeline philosophy.

Limitations

  • Single Functionality: Primarily used for viewing the beginning of files.

  • Lacks Interactivity: Unlike less or more, it does not support interactive browsing.

  • Fixed Range: The display range is determined before command execution.

Best Practice Recommendations

  • Clearly Specify Display Count: Always use -n or -c to explicitly specify the display range.

  • Handle Multiple Files: Use -q or -v to control file name display.

  • Combine with Other Tools: Make good use of pipes and other commands.

  • Error Handling: Check file existence and permissions in scripts.

  • Performance Consideration: For extremely large files, prefer using head instead of loading the entire file.

Comparison with Other Commands

# head vs tail head -n 10 file    # View the first 10 lines tail -n 10 file    # View the last 10 lines # head vs less/more head -n 100 file   # Quickly view the first 100 lines less file          # Interactive browsing of the entire file

In modern workflows, mastering the head command can significantly enhance efficiency in command-line environments, especially when dealing with large files and data analysis tasks.

#Linux Command #head

[If there are any omissions, please correct them!]

Leave a Comment