head — Linux basic command
Linux / Basic Linux commands / head.html
A command that prints only the beginning part (the first few lines) of a file.
Basic Information
| Command Name | head |
|---|---|
| One-line Introduction | A command that prints only the beginning part (the first few lines) of a file. |
| Provided by | GNU coreutils (installed by default in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/head.1.html |
| Supported OS | Linux, macOS, BSD, WSL |
| Installation Status | Installed by default (no separate installation required) |
| Category | Linux Basic Command (Tags: file output, text, line count, linux, basic) |
What is this command?
head is a command that outputs only the specified number of lines (default 10 lines) from the beginning of a file. It is used to quickly check the first part of a long file.
You can specify the number of output lines with the -n option, and also output based on the number of bytes with the -c option.
What problem does it solve?
- Checks the beginning of a long file.
- Quickly checks header information.
- Looks at the beginning part to understand the file structure.
- Previews only the front part of a large file.
Main Features
Default 10-Line Output
Prints the first 10 lines of a file.
- Use Case: When you need to quickly grasp the file structure.
- Result Obtained: You can view the content at the beginning of the file.
- Limitations: If the file has fewer than 10 lines, the entire content is printed.
Specify Number of Lines (-n)
Specify the number of lines to output with the -n option.
- Use Case: When you only want to check a specific number of lines.
- Result Obtained: Outputs exactly the specified number of lines.
- Limitations: An error occurs if a value of 0 or less is specified.
Output by Byte Count (-c)
Outputs based on the number of bytes using the -c option.
- Use Case: When reading fixed-length record files.
- Result Obtained: Outputs the specified number of bytes.
- Limitations: Multi-byte characters like Hangul may be truncated.
Simultaneous Output of Multiple Files
Outputs the beginning parts of multiple files in sequence.
- Use Case: When checking the beginning of multiple log files at once.
- Result Obtained: Outputs each file with its header.
- Limitations: Output can become lengthy if there are many files.
Installation and Execution
Verify Default Installation
It is included by default in all Linux distributions.
which head
head --version
Installation (Lightweight Environment)
Typically already installed.
sudo apt install coreutils # Debian/Ubuntu
Practical Usage Examples
Default Output
Check the first 10 lines of a file.
# Print the first 10 lines
cat /var/log/syslog | head
# Specify file directly
head /var/log/syslog
# Multiple files
head file1.txt file2.txt
Specify Number of Lines
Outputs the desired number of lines.
# Only the first 5 lines
head -n 5 /var/log/syslog
# Only the first 20 lines
head -20 /var/log/syslog
# With a pipe
ps aux | head -5
Output by Byte Count
Outputs based on the number of bytes.
# Print only the first 100 bytes
head -c 100 /var/log/syslog
# Only the first line (head -1)
head -1 /etc/passwd