The head
command has long been a go-to tool for quickly inspecting the start of a file in Linux. Here in 2025, its usefulness hasn’t diminished—if anything, it’s only become more important for large-scale data analysis and server administration. Whether you’re dealing with massive logs, configuration files, or script outputs, head
allows you to preview file content without loading everything at once.
If you also want to see new log entries as they appear, check out our quick tip on following file changes in real time. By combining head
with other commands, you can locate relevant information faster and create flexible solutions for log analysis and system administration.
Table of Contents
Why head
Remains Essential in 2025
- Simplicity: Its core function—displaying the first lines of a file—remains straightforward and effective.
- Performance: Large log files have only grown bigger with time. Being able to glance at initial lines or characters saves precious time and system resources.
- Versatility: By combining
head
with commands likegrep
ortail
, you can pinpoint relevant data in seconds instead of scrolling through thousands of lines.
If you’ve ever used the command line to dig through large configuration files or logs, you already understand the importance of speed and efficiency. head
excels at both, and it fits neatly into automated scripts and pipelines.
Using head
Across Popular Linux Distributions
Regardless of whether you’re on Ubuntu, Fedora, CentOS, or RHEL, the syntax and functionality remain largely the same. This consistency means you can confidently apply the examples below on most Linux systems.
- Quick Preview in Ubuntu:
head -n 5 /var/log/syslog
- Quick Preview in CentOS:
head -n 5 /var/log/messages
These commands give you the first five lines of your main system logs, a fast way to see if something is immediately wrong.
Displaying the First N Characters Using head
Command
Sometimes, you just want a sneak peek of the text in a file—especially if it’s a massive log or a large dataset. The -c
flag is your friend here:
head -c 50 filename
By including -c 50
, you see just the first 50 characters of filename
. This can confirm if you’re looking at the right file format or if a specific string appears early in the document. If you ever need to adjust file names before using head
, don’t forget you can bulk change file extensions in Linux using shell commands.
(Tip: For extremely large files, consider adding | less
after head
to view the output page by page.)
Combining head
with tail
for Specific Ranges
A common scenario is wanting lines 10 through 20 of a file. Instead of using an editor, you can get those lines quickly by piping head
into tail
:
head -20 filename | tail -11
- Steps:
head -20
selects the first 20 lines.tail -11
selects the last 11 lines from that selection.
- Result: You see lines 10 through 20 of
filename
.
This approach helps if you’re searching specific sections in a massive log. For continuous monitoring of a log file instead, check out tail -f
, which we cover in greater detail in our dedicated tail command tutorial.
Using head
with grep
Another powerful technique is to pair head
with grep
to quickly find and preview lines containing certain patterns. This is especially useful for log analysis or data wrangling:
grep "search_word" filename | head -n 10
- What It Does: Searches for “search_word” in
filename
and displays only the first 10 matching lines. - Why It Matters: When you’re troubleshooting errors or investigating logs on a busy server, this approach narrows your results quickly without overwhelming you.
(Pro Tip: Combine this technique with pipes to other commands, such as sort
or uniq
, for even deeper filtering.)
Displaying Multiple Files at Once with the head
Command
If you need to see a preview of more than one file, head
can handle that in a single command:
head file1 file2 file3
- How It Works:
head
shows the first 10 lines (by default) offile1
, thenfile2
, thenfile3
.- Each block of output is clearly labeled with the file name.
- Real-World Use Case: Quickly review log files from multiple services to see if there are any immediate indicators of errors in the first few lines.
Adjusting the Amount of Output
By default, head
displays the first 10 lines. However, you might need more—or fewer. That’s where the -n
option shines:
head -n 15 filename
- Effect: This displays the first 15 lines of
filename
. - When to Use: If you suspect issues in the first 20 lines of a config file, for instance,
head -n 20
can save you time compared to opening an entire file in an editor.
(Pro Tip: For very large files in data analytics, you could do something like head -n 100 largefile.csv
to quickly confirm the file’s structure, column headers, or data formatting.)
Advanced Usage in Scripts
head
truly shines when it’s part of a larger automated workflow. By integrating it into shell scripts or cron jobs, you can ensure that only relevant data is processed, preventing memory overload and saving time. For instance, you might head
the first 50 lines of a log file to quickly check for errors before sending the output to another utility like grep
or awk
. If you’re looking to create your own automated tasks and schedule them effectively, learn how to write a Bash script for step-by-step guidance.
- Partial File Processing: Use
head
to load only a small part of a file before performing transformations with tools likeawk
orsed
. - Error Reporting: In a cron job, you can send just the first 20 lines of a recent log to your inbox if it contains certain keywords (use
grep
+head
). - Quality Checks in Data Pipelines: Validate that your CSV or JSON files start with the correct header info without loading the entire dataset.
These methods help you manage modern data-heavy environments more efficiently—a must for any sysadmin or DevOps engineer.
Advanced Examples: Combining head
and grep
Find the First File Containing a Specific Word
If you’re unsure which file references a certain term, use this command to search for “search_word” in the specified directory and print just the first file that matches:
grep -rl "search_word" /path/to/directory | head -n 1
Extract the First N Lines Containing a Specific Word
When you only want a quick look at the earliest matches, locate the first few lines containing “search_word” by piping grep
into head
:
grep "search_word" filename | head -n 10
Display the First File That Contains a Certain Pattern
To find the initial file that includes a particular pattern, run this search in the current directory:
grep -rl "pattern" * | head -n 1
Find the First N Occurrences of a Pattern Across Multiple Files
If you need to scan multiple directories or a large codebase for a pattern, rely on a recursive search and limit how many matches head
displays:
grep -r "pattern" /path/to/directory | head -n 10
Display a Specific Number of Log Entries Containing a Certain String
Quickly filter Apache logs or other server logs for a particular error string without being inundated by endless lines:
grep "Error 404" /var/log/apache2/access.log | head -n 20
These approaches can significantly streamline your workflow by combining the filtering power of grep
with the quick-preview functionality of head
. They’re especially useful when dealing with massive logs, large projects, or multiple directories in fast-paced environments.
If you want to confirm how many files exist in a directory before using head
, learn how to count the number of files in a Linux folder. This extra check helps you avoid scanning directories that might not even contain the data you need.
Pro Tips for Modern Data Workflows
- Batch Processing: In today’s systems, data sets can be enormous. Using
head
ensures you don’t overwhelm system memory by loading entire files at once. - Automation: Place these commands in scripts or CI pipelines to automatically flag or log the first occurrences of an error.
- Version Control & Auditing: If you store logs or config files in a version control system,
head
can help quickly preview changes introduced in new commits.
Below is a short additional section that includes two advanced scripting examples. This helps illustrate how head
can be integrated into automated workflows and further establishes your authority on the topic.
Bonus: Advanced head
Command Examples in Scripts
Automated Log Monitoring with Cron
If you want to check the first 10 lines of a log file every day and receive an email alert, you can use a simple shell script like this:
#!/bin/bash
# Automated daily check of the first 10 lines in /var/log/syslog
# and send them via email
head -n 10 /var/log/syslog | mail -s "Daily Log Check" [email protected]
- How to Use
- Save this script (e.g.,
daily_syslog_check.sh
), make it executable withchmod +x daily_syslog_check.sh
, and place it in a suitable directory. - Add it to your
crontab
(e.g.,0 6 * * * /path/to/daily_syslog_check.sh
) to run every morning at 6 AM.
- Save this script (e.g.,
- Why It Matters
- Automatically capturing the first lines of a log can reveal startup errors or recurring issues quickly.
- Sending the output via email provides immediate insight without manually logging into the server.
Quality Assurance for Data Files
When working with large CSV or JSON files, you might only need to confirm that the headers or initial fields match the expected format:
#!/bin/bash
# Check the first 5 lines of a CSV file to validate headers and structure
# before processing the entire file
FILE_PATH="/path/to/data.csv"
if [ -f "$FILE_PATH" ]; then
echo "Previewing $FILE_PATH ..."
head -n 5 "$FILE_PATH"
else
echo "File not found: $FILE_PATH"
fi
- What This Script Does
- Verifies that the file exists, then uses
head
to display the first five lines. - This is useful in data pipelines to confirm headers (e.g., “Name,Email,Age”) before bulk-processing.
- Verifies that the file exists, then uses
- Real-World Benefit
- Prevents you from parsing corrupt or mislabeled files.
- Saves computational resources by checking only a small portion of the file first.
By incorporating scripts like these into your daily workflow, you’ll streamline repetitive tasks and catch potential issues early—further showcasing just how versatile and powerful the head
command can be in modern Linux environments.
Frequently Asked Questions (FAQ)
What is the maximum file size head
can handle?
There’s technically no hard limit on file size for the head
command as long as your file system can handle it. Since head
only reads from the start of the file, it’s not drastically affected by overall file size—it just matters how many lines or characters you request.
What’s the main difference between head
and tail
?
head
displays the beginning of a file (by default, the first 10 lines).tail
shows the end of a file (by default, the last 10 lines).
Both are invaluable for previewing large files without fully loading them, but they focus on different sections.
Can I use head
in automated scripts?
Absolutely. Many sysadmins and developers rely on head
in shell scripts to check log headers, confirm file format, or grab partial data before passing it along to other commands. Its simplicity and low overhead make it perfect for automated tasks.
Is head
part of every Linux distribution?
Yes, head
is part of the GNU core utilities (coreutils) and is present on virtually all Linux distributions, including Ubuntu, Fedora, CentOS, RHEL, and even minimal container images.
Final Thoughts
The head
command continues to be a staple in any Linux toolbox. Its ability to preview file content efficiently, especially when combined with powerful utilities like grep
and tail
, makes it essential for modern server administration and data processing. With these tips, tricks, and best practices, you’ll save time, reduce errors, and maintain a cleaner workflow across your Linux environment.