Linux head Command

Display the first lines of files

Overview

The head command displays the first lines of files. By default, it shows the first 10 lines of each file, making it useful for quickly previewing file contents, examining log files, or processing text data.

Key Features:
  • Display first N lines of files
  • Process multiple files simultaneously
  • Work with standard input
  • Byte-based output options

Syntax

head [OPTIONS] [FILE...]

Common Options

Option Description
-n NUM Display first NUM lines (default: 10)
-c NUM Display first NUM bytes
-q Quiet mode - never print headers
-v Verbose mode - always print headers
--help Display help information
--version Display version information

Basic Examples

Display First 10 Lines (Default)

head file.txt
Line 1 of the file
Line 2 of the file
Line 3 of the file
...
Line 10 of the file

Display Specific Number of Lines

head -n 5 file.txt
Line 1 of the file
Line 2 of the file
Line 3 of the file
Line 4 of the file
Line 5 of the file

Display First N Bytes

head -c 50 file.txt
First 50 characters of the file content...

Advanced Examples

Multiple Files

head file1.txt file2.txt file3.txt
==> file1.txt <==
First 10 lines of file1
...

==> file2.txt <==
First 10 lines of file2
...

==> file3.txt <==
First 10 lines of file3
...

Suppress Headers for Multiple Files

head -q *.txt
Content from all .txt files without headers

Always Show Headers (Even for Single File)

head -v file.txt
==> file.txt <==
First 10 lines of the file

Using with Standard Input

ls -la | head -n 5
total 48
drwxr-xr-x 8 user user 4096 Jan 15 10:30 .
drwxr-xr-x 25 user user 4096 Jan 15 10:25 ..
-rw-r--r-- 1 user user 220 Jan 15 10:25 .bash_logout
-rw-r--r-- 1 user user 3771 Jan 15 10:25 .bashrc

Practical Use Cases

1. Log File Analysis

# Check recent log entries
head -n 20 /var/log/syslog

# Monitor multiple log files
head -n 5 /var/log/*.log

2. Configuration File Preview

# Preview configuration files
head -n 15 /etc/nginx/nginx.conf

# Check file headers
head -c 100 data.csv

3. Data Processing

# Sample data from large files
head -n 1000 large_dataset.txt > sample.txt

# Combine with other commands
head -n 50 data.txt | grep "pattern"

4. Script Output Monitoring

# Monitor script output
./script.sh | head -n 20

# Check process list
ps aux | head -n 10

Tips and Best Practices

Performance Tips:
  • Use head instead of opening large files in editors
  • Combine with grep for pattern matching in file headers
  • Use byte mode (-c) for binary file inspection
  • Pipe output to other commands for further processing
Common Pitfalls:
  • Remember that line counting starts from 1, not 0
  • Be careful with binary files - use -c option
  • Headers are shown by default for multiple files
  • Empty files will produce no output

Related Commands

File Viewing:
  • tail - Display last lines of files
  • less - View file contents page by page
  • more - Display file contents screen by screen
Text Processing:
  • cut - Extract columns from text
  • grep - Search text patterns
  • sed - Stream editor for text manipulation

Frequently Asked Questions

The head command displays the first lines of files. By default, it shows the first 10 lines of each file, making it useful for quickly previewing file contents without opening the entire file.

Use the -n option followed by the number of lines: head -n 20 filename. You can also use the shorthand format: head -20 filename.

Yes, head can process multiple files simultaneously. When multiple files are specified, head displays headers showing the filename for each file. Use -q to suppress headers or -v to always show them.

The -n option specifies the number of lines to display, while -c specifies the number of bytes (characters) to display. Use -c when you need precise byte-level control or when working with binary files.

Head works well with pipes to process command output: ls -la | head -n 5 shows the first 5 lines of directory listing. You can also pipe head's output to other commands: head -n 20 file.txt | grep "pattern".