Loading date…
LinkedIn Twitter Instagram YouTube WhatsApp

head Command in Linux: Fast Log Triage for SOC Analysts

SOC analyst using the Linux head command to triage a large server log file during incident response

The head Command in Linux: A SOC Analyst's Fast-Track Tool for Log Triage and Incident Response

It's 2:47 AM and a SOC analyst on the graveyard shift just got paged for a spike in failed SSH logins on a production jump box. The auth log is over 400,000 lines long by the time she opens it. She doesn't have time to scroll. She doesn't need the whole file. She needs the first burst of events — the moment the attack started — before the noise buries the signal. One command gets her there in under two seconds: head.

Most people learn head as a beginner's Linux command and move on. But in a real SOC environment, head is a first-response triage tool. It's how analysts sample massive log files, verify data before running expensive parsing jobs, and confirm the shape of an incident before committing to a full investigation. This guide walks through head the way it's actually used on the floor — not just the syntax, but the reasoning behind it.

Table of Contents

What head Actually Does

Diagram showing the Linux head command displaying the first 10 lines of a large log file versus cat showing the entire file

head is a core Linux/Unix utility that prints the beginning of a file or input stream. By default, it shows the first 10 lines — but every flag changes what "beginning" means, and that flexibility is exactly why it's still relevant on modern SOC workstations, cloud bastion hosts, and forensic images decades after it was written.

Unlike cat, which dumps an entire file, head is deliberately limited. That limitation is the feature. When you're staring down a multi-gigabyte log file over a slow SSH session, you don't want the whole thing — you want a fast, safe preview.

Why SOC Analysts Rely on head

SOC analyst reviewing auth logs, firewall logs, and SIEM data using the head command before running grep or building a Splunk query

Security logs — auth logs, web server access logs, firewall logs, EDR exports — grow fast and grow ugly. A single compromised host can generate hundreds of thousands of lines in a few hours during an active attack. Before an analyst runs grep, builds a Splunk query, or ships logs to a SIEM, they usually eyeball the file structure first. That's where head comes in:

  • Confirming log format before writing a parsing script (CSV headers, JSON structure, syslog format)
  • Sampling the start of a suspicious file without triggering a full read (important on resource-constrained incident response boxes)
  • Checking timestamps at the top of a log to establish a rough starting point for a timeline
  • Verifying that a log export or SIEM dump actually contains data before trusting downstream automation

Core head Commands With Real Explanations

Terminal screen showing Linux head command examples used by SOC analysts to filter and preview log file contents

Show the first 10 lines (default behavior)

head file.txt

What it does: Prints the first 10 lines of the file. When to use it: Your default move when opening any unfamiliar log for the first time — a quick sanity check before deeper analysis. Expected output: The literal first 10 lines, unmodified, in original order.

Show the first N lines

head -n 5 file.txt

What it does: Displays exactly 5 lines instead of the default 10. When to use it: Useful when you only need a header row or a small sample, like checking a CSV export's column structure. Expected output: The first 5 lines exactly.

View multiple files at once

head file1.txt file2.txt

What it does: Shows the first 10 lines of each file, clearly labeled with filenames. When to use it: Comparing multiple log exports from different hosts during a multi-system incident, like checking whether all affected endpoints show the same initial attack signature. Expected output: Filename headers followed by each file's first 10 lines.

Display the first N bytes

head -c 20 file.txt

What it does: Prints the first 20 bytes rather than lines. When to use it: Critical for binary analysis — checking a file's magic bytes/header to identify true file type, which matters when malware is disguised with a fake extension. Expected output: Raw byte content, often unreadable for non-text files, which itself is diagnostic information.

Combine with cat via a pipe

cat file.txt | head -n 15

What it does: Pipes the full file content through head, printing only the first 15 lines. When to use it: Mostly seen in scripts chaining multiple commands together, though direct head -n 15 file.txt is more efficient on its own. Expected output: The first 15 lines of the file.

Show all except the last N lines

head -n -5 file.txt

What it does: Prints everything except the last 5 lines. When to use it: Trimming incomplete trailing entries from a log that's still being written to, so you don't analyze a partial, mid-write line. Expected output: The full file minus the final 5 lines.

Show all except the last N bytes

head -c -20 file.txt

What it does: Displays the file content minus the last 20 bytes. When to use it: Rare, but useful in forensic byte-level trimming of truncated or corrupted binary evidence. Expected output: All bytes except the final 20.

Read from standard input

echo -e "one\ntwo\nthree" | head -n 2

What it does: Takes piped input instead of a file and returns the first 2 lines. When to use it: Common when chaining live command output (like process lists or network connections) rather than static files. Expected output: "one" and "two".

View the beginning of a log file

head -n 50 /var/log/syslog

What it does: Shows the first 50 lines of the system log. When to use it: Checking when logging started for the current log file, useful when correlating log rotation timing with an incident window. Expected output: 50 lines of syslog entries with timestamps.

Preview a CSV file

head -n 10 data.csv

What it does: Shows the first 10 lines including the header row. When to use it: Before importing threat intel feeds or IOC lists into a SIEM, to confirm column order and delimiter. Expected output: Header row plus 9 data rows.

Combine with grep

grep "ERROR" logfile.log | head -n 10

What it does: Filters for lines containing "ERROR", then limits output to the first 10 matches. When to use it: One of the most common SOC one-liners — quickly sampling error or alert patterns without flooding the terminal. Expected output: Up to 10 matching lines.

Save the first lines to a file

head -n 10 file.txt > preview.txt

What it does: Redirects the first 10 lines into a new file called preview.txt. When to use it: Creating a lightweight sample file to share with a teammate or attach to a ticket, without sending an entire multi-GB log. Expected output: A new file, preview.txt, containing 10 lines.

View the beginning of multiple logs

head -n 5 *.log

What it does: Shows the first 5 lines of every .log file in the current directory. When to use it: Fast structural check across an entire log directory during initial incident scoping. Expected output: Filename headers followed by 5 lines per log file.

Show the first bytes of a binary file

head -c 16 file.bin

What it does: Displays the first 16 bytes of a binary file. When to use it: Quick manual file-type verification — many file formats have unique magic bytes in their first few bytes (PE, ELF, ZIP, PDF signatures). Expected output: Often garbled text/hex-like output representing raw binary data.

Real-World Scenario: Triaging a Brute-Force Alert

SOC analyst investigating a 1.2 million line auth.log file for a brute-force SSH attack using head and grep commands to find failed password attempts

Consider a mid-sized SaaS company running Ubuntu servers behind a bastion host. The SIEM fires an alert: unusual volume of failed SSH authentication attempts against a single production server. The on-call analyst SSHes into the box and finds /var/log/auth.log sitting at 1.2 million lines.

Instead of opening the whole file in a text editor (which could hang the session or eat memory on a resource-limited box), the analyst runs:

head -n 20 /var/log/auth.log

This confirms the log's current rotation start time and format. Next, to see when the failed attempts actually began, they combine it with grep:

grep "Failed password" /var/log/auth.log | head -n 10

Within seconds, the first 10 failed login attempts appear — complete with source IPs and usernames. That's the starting timestamp the analyst needs to build an incident timeline, correlate with firewall logs, and determine whether the attack is still in progress or already over.

Using head in Detection and Threat Hunting Workflows

Threat hunter using the head command to validate log formats, check IOC feeds, and verify malware file types via magic bytes during a lateral movement investigation

head rarely works alone — it's a supporting tool in a larger detection chain. Some patterns seen regularly in SOC playbooks and threat hunting runbooks:

  • Log format validation: Before ingesting a new log source into a SIEM, analysts run head to confirm the field structure matches the expected parser configuration.
  • IOC feed sanity checks: Threat intel feeds delivered as CSV or JSON get a quick head pass to confirm they downloaded correctly and aren't truncated or corrupted.
  • File type verification during malware triage: Combined with -c, analysts check magic bytes to confirm a suspicious file isn't disguised with a mismatched extension — a classic evasion technique.
  • Rapid multi-host comparison: During a suspected lateral movement incident, running head -n 5 *.log across collected logs from multiple hosts quickly reveals whether they share a common attack signature at the top of each file.

Common Mistakes and Pitfalls

Warning icons highlighting common head command mistakes like ignoring log rotation, missing tail data, and mishandling binary byte output
  • Assuming the first lines represent the whole incident: head only shows the start of a file — pair it with tail or full-text search to avoid missing the most recent, and often most relevant, activity.
  • Forgetting log rotation: The "beginning" of a rotated log file might not align with when the incident actually started — always confirm rotation timestamps first.
  • Using head on live-writing files without accounting for buffering: On files actively being written to, the last few lines can be incomplete; head -n -N can help avoid analyzing partial entries.
  • Treating byte output as safe to paste anywhere: Binary byte output from head -c can contain non-printable characters that break terminal formatting or corrupt logs if redirected carelessly.

Expert Tips From the Field

Expert cybersecurity tips for using head with wc and jq commands, plus best practices for forensic evidence handling during incident response
  • Pair head with wc -l first to know the total line count before deciding how much of the file actually needs sampling.
  • For JSON logs, combine head with jq to preview only the first few well-formatted records instead of raw truncated JSON.
  • When documenting an incident, always note the exact head command used in your case notes — reproducibility matters during post-incident review.
  • On forensic images, always work from a copy — never run commands, even read-only ones like head, directly against original evidence.

Related Cybersecurity Topics You Should Explore

FAQ

Q1: What's the difference between head and cat?
cat prints an entire file's contents, while head limits output to a specified number of lines or bytes from the beginning — making it safer for large files.

Q2: Can head be used on live log files that are actively being written to?
Yes, though for actively growing files, tail -f is typically preferred for watching new entries in real time; head is best for a one-time snapshot of the start.

Q3: Is head available on Windows?
Native Windows lacks head, but it's available through WSL, Git Bash, or Cygwin, and PowerShell offers a similar function via Get-Content -TotalCount.

Q4: Why would a SOC analyst use head -c instead of head -n?
Byte-level output is essential for binary file inspection, such as checking magic bytes to verify true file type during malware analysis.

Q5: Does head modify the original file?
No, head is a read-only command — it never alters or deletes the source file, making it safe to run on evidence and live systems alike.

Q6: How does head handle negative numbers like -n -5?
A negative value tells head to print everything except the last N lines, effectively excluding the tail end rather than limiting the head.

Conclusion

The head command looks trivial on paper — ten lines, a file, done. But in a real SOC workflow, it's one of the fastest ways to sanity-check data before committing time and compute to deeper analysis. Whether it's confirming a log's format before parsing, spotting the first signs of a brute-force attempt, or verifying a suspicious file's true type through its magic bytes, head earns its place in every analyst's daily toolkit. Master the fundamentals, and it becomes second nature during the moments that matter most — the first few minutes of an incident, when speed and clarity decide everything that follows.

Shubham Chaudhary

Welcome to Xpert4Cyber! I’m a passionate Cyber Security Expert and Ethical Hacker dedicated to empowering individuals, students, and professionals through practical knowledge in cybersecurity, ethical hacking, and digital forensics. With years of hands-on experience in penetration testing, malware analysis, threat hunting, and incident response, I created this platform to simplify complex cyber concepts and make security education accessible. Xpert4Cyber is built on the belief that cyber awareness and technical skills are key to protecting today’s digital world. Whether you’re exploring vulnerability assessments, learning mobile or computer forensics, working on bug bounty challenges, or just starting your cyber journey, this blog provides insights, tools, projects, and guidance. From secure coding to cyber law, from Linux hardening to cloud and IoT security, we cover everything real, relevant, and research-backed. Join the mission to defend, educate, and inspire in cyberspace.

Post a Comment

Previous Post Next Post
×

🤖 Welcome to Xpert4Cyber

Xpert4Cyber shares cybersecurity tutorials, ethical hacking guides, tools, and projects for learners and professionals to explore and grow in the field of cyber defense.

🔒 Join Our Cybersecurity Community on WhatsApp

Get exclusive alerts, tools, and guides from Xpert4Cyber.

Join Now