How Do head and tail Work — Extracting the First or Last Lines of Text

head and tail — Chunky Munster

Head and tail are the blunt instruments of text inspection: one grabs the start, the other grabs the end. If you need the first few log lines, the last rows of a CSV export, or a quick sample from a giant paste, try our free head and tail tool and cut the noise down to size.

What head and tail actually do

In Unix and shell land, head returns the first lines of a file or stream, while tail returns the last lines. They do not parse columns, understand JSON, or care whether the text is logs, code, or a spreadsheet export that someone renamed to .txt. They just keep the lines you asked for and discard the rest.

That simplicity is why they show up everywhere. If a deployment explodes, the useful clue may be in the first ten lines of the log. If a process dies after printing a stack trace, the last few lines may be all you need. The whole point is to avoid opening a giant file and pretending your scroll wheel is a debugging strategy.

Why developers reach for them first

For quick inspection, head and tail are often faster than searching or loading the whole file into an editor. They are especially handy when the input is huge, remote, or messy enough that you only want a narrow sample before deciding what to do next.

Common cases:

In a terminal, the classic forms look like this:

head app.log
head -n 20 export.txt
tail server.log
tail -n 15 results.csv

The browser version follows the same logic, but without shell syntax or command-line memory gymnastics. Paste text, choose how many lines you want from the top or bottom, and you get a clean slice in seconds.

When to use head versus tail

Use head when the useful context lives near the beginning. That happens a lot with structured files, boot logs, CSV exports, and command output that starts with a header or metadata block.

Use tail when the interesting part is usually the most recent output. Logs, event streams, append-only files, and anything that grows over time tend to fit that pattern. If a job is still writing to a file, the bottom is usually where the action is.

A good rule: ask whether you care about the first evidence or the latest evidence. If you are checking format, use head. If you are checking what happened last, use tail.

How this maps to logs, exports, and pasted text

Head and tail work best on line-oriented text. That means log files, CSVs, TSVs, config dumps, stack traces, and plain text copied from tools or terminals. If your data has line breaks, these filters are useful; if it is a single long line, they are much less exciting.

One small trap: many exports include a header row. If you only inspect the first line, you may see column names and assume the data is fine. Better to use head with a few lines so you can verify both the schema and the shape of the records underneath it.

If you are trimming or cleaning the content before slicing it, a related helper like our guide on keeping only the lines you need from text may help when the input needs filtering before it gets sliced.

Browser workflows that save time

In a browser, the main advantage is speed without setup. You do not need to remember flags, pipe anything, or install a tool just to check whether the first five lines look sane. That makes it useful for support work, data review, and quick debugging from a pasted sample.

A few practical habits help:

  1. start with a small slice, usually 5 to 10 lines
  2. increase the count only if the context is still missing
  3. use head for formats with headers or initialization data
  4. use tail for append-only logs and crash output

If the text is already cleaned up but still huge, a head/tail pass is often enough to decide whether you need deeper tools. It is the text equivalent of cracking a safe just enough to see whether there is anything worth taking.

A Worked Example

Say you have a service log and want to confirm both the startup banner and the last error message. You do not need the entire file. You need two tiny windows into it.

2026-07-14 09:11:02 INFO  starting worker 3
2026-07-14 09:11:02 INFO  loading config from /etc/app/config.yml
2026-07-14 09:11:03 INFO  connected to postgres
2026-07-14 09:11:04 INFO  cache warmed
2026-07-14 09:11:05 INFO  listening on :8080
2026-07-14 09:19:41 WARN  request timeout for /api/search
2026-07-14 09:19:42 ERROR connection reset by peer
2026-07-14 09:19:42 ERROR retry exhausted
2026-07-14 09:19:43 INFO  shutting down worker 3

With head, you might pull the first 3 lines to confirm the service booted normally:

2026-07-14 09:11:02 INFO  starting worker 3
2026-07-14 09:11:02 INFO  loading config from /etc/app/config.yml
2026-07-14 09:11:03 INFO  connected to postgres

With tail, you might pull the last 3 lines to see how it ended:

2026-07-14 09:19:42 ERROR connection reset by peer
2026-07-14 09:19:42 ERROR retry exhausted
2026-07-14 09:19:43 INFO  shutting down worker 3

That gives you the bookends of the problem without reading every line. If the issue turns out to be in the middle, you can widen the window or move to a more specific search tool.

How head and tail differ from truncating or reversing

Head and tail are not the same as chopping text at a character count. They operate on lines, not bytes or words. That matters because line-based tools preserve the structure of logs and tabular text in a way that raw truncation does not.

They also differ from reversing line order. If you reverse a log and then take the top lines, you are still changing the meaning of what you see. Tail keeps the end in its original order, which is what you usually want when you are reading recent events.

In other words, head and tail are for inspection, not transformation. They answer a simple question: what does the start or end look like right now?

Frequently Asked Questions

What is the difference between head and tail in Unix?

Head prints the beginning of a file or stream, and tail prints the end. Both work line by line and are commonly used to inspect logs, exports, and plain text quickly. They are filters, not parsers, so they do not interpret the content.

How do I get the first 10 lines of a file?

Use head on the command line, usually head -n 10 filename. In a browser tool, paste the text and set the line count to 10. If the file has a header row, remember that the first 10 lines may include metadata, not just data rows.

How do I get the last 10 lines of a file?

Use tail -n 10 filename in Unix-like systems. That is the standard way to inspect the most recent log entries or the bottom of an export. If the file is still growing, tail is usually the better choice because it tracks the end.

Are head and tail available on Windows?

Not as standard built-ins in classic Command Prompt, but they are available in PowerShell or through Unix-like shells such as Git Bash and WSL. In a browser, you do not need any of that. You can paste the text and slice it by line count directly.

Wrapping Up

Head and tail are small tools with a very specific job: show you the first or last lines without dragging the whole file into view. That makes them ideal for logs, exports, pasted text, and any line-based data that needs a quick sanity check.

If you are debugging startup output, checking the end of a failed job, or just trying to see whether a giant paste is worth deeper inspection, start there. Then, if you need more than the bookends, widen the slice or move on to search, filtering, or line-range tools.

When you want the browser version, use this head and tail tool and keep the terminal muscle memory for something else.

// try the tool
try our free head and tail tool →
// related reading
← all posts