head and tail from Zero: Read the Start and End of a File, and Lose No Lines to Rotation
Reading a log means caring about its two ends: what happened at the start and where it stopped. This guide starts from zero on head and tail: the 10-line default, the sign trap in -n, the -c trap that cuts Chinese characters in half, tail -f following a file that is still growing, and -F following the name after rotation. Every step was run for real on GNU coreutils 9.4.

When you read logs, error output, or the result of a run, nine times out of ten you only care about two ends: what the first few lines said, and where the last few lines stopped. A file can be tens of thousands of lines long, and reading all of it is neither realistic nor necessary. That is what head and tail are for: head shows the start of a file, tail shows the end. Both ship with GNU coreutils, so they are already on any Linux machine with nothing to install. This guide starts from zero on head and tail: the default behaviour, the trap in the signs you can put after -n, the trap in -c counting bytes instead of lines, the headers you get with several files, how tail -f follows a file that is still growing, and the difference between -f and -F once logs rotate. Every step was run for real on GNU coreutils 9.4, and every line of output quoted here matches the test record at the end of the article word for word.
The test file is called lines.txt: 20 lines built with a single command, line-01, line-02, all the way to line-20, with the contents fixed in advance so they can be counted exactly. There is also a Chinese file, cn.txt, with just three lines: 第一行, 第二行, 第三行. First, confirm the line count: wc -l lines.txt prints 20 lines.txt. Everything that follows runs on these two files, so you can repeat it on your own machine and get the same results.
1. Ten lines by default: head reads the start, tail reads the end
With no options, head prints the first 10 lines. In the test, head lines.txt printed line-01 through line-10, exactly 10 lines, and nothing from line-11 onward. tail does the opposite: tail lines.txt printed the last 10 lines, line-11 through line-20. Neither command changes the file; it only reads it out for a look, and the file stays exactly as it was.
To change the number of lines, use -n. head -n 3 lines.txt prints only the first 3 lines: line-01, line-02, line-03. tail -n 3 lines.txt prints only the last 3 lines: line-18, line-19, line-20. The number after -n is simply "how many lines", nothing more. head works the same way reading from a pipe: seq 1 5 | head -n 2 prints 1 and 2; seq produces 5 lines and head keeps the first two and drops the rest. With no file argument, head and tail both read standard input, so they work at the end of a pipe too. That is one of their most common uses: another command produces output, and head or tail cuts out a slice to look at.
The question beginners always ask is: where does the default of 10 lines come from? The manual states it plainly: head prints the first part of a file, 10 lines by default; tail prints the last part, 10 lines by default. It is a convention, not something computed. Ten lines is small enough to take in at a glance and big enough to show what happened at the start and where things stopped at the end. When you read a log, first tail the end to check whether the program exited normally, then head the start to check where it began. Those two steps are the usual routine.
When a program has crashed and you need the cause, the order is the same: look at the tail first. The last few lines before a crash usually are the error itself, or the last normal output right before it. tail -n 50 on the log file shows the last 50 lines; if there is an error in there, follow it backwards from that point, and if there is not, widen the range. When a program fails to start at all and has barely written any log, the head is more useful: one look shows whether it read the wrong config file or quit at the very first step. Look at the tail to judge what happened, then look at the head to find where it started; once those two steps are done, most problems already have a direction.
2. The signs after -n: minus and plus mean completely different things
This is the first real trap in this guide. The number after -n can carry a sign, and a minus sign means something very different from a plus sign — and head and tail do not even agree on what the signs mean. head first: head -n -3 lines.txt printed line-01 through line-17, 17 lines in total. The -3 does not mean "take 3 lines"; it means "everything except the last 3 lines". The file has 20 lines, so dropping the last 3 leaves 17, and all 17 were printed. The manual says it directly: if the number is prefixed with a '-', print all but the last that many lines of each file.
Now tail: tail -n +6 lines.txt means start printing at line 6 and continue to the end of the file. The test counted it with wc -l: tail -n 6 lines.txt | wc -l printed 6, while tail -n +6 lines.txt | wc -l printed 15. One plus sign turns 6 lines into 15: the first is "the last 6 lines", the second is "everything from line 6 onward", which is line-06 through line-20, 15 lines in total. The + means "start here", and the manual puts it this way: if the number is prefixed with a '+', start printing with line that many from the start of each file.
The easiest way to get caught is when the two commands print exactly the same output. tail -n 3 lines.txt prints line-18, line-19, line-20, and tail -n +18 lines.txt prints line-18, line-19, line-20 as well. Same output, completely different meanings: the first is "the last 3 lines" and will only ever take the tail no matter how long the file is; the second is "from line 18 onward", which would print 83 lines out of a 100-line file. Looking at one run you cannot tell them apart; change the file length and the difference shows up at once. Remember the rule: with tail, -n without a sign means "take the last few lines", and with a plus sign it means "start at this line". Skipping the header lines of a file before processing the data uses exactly this form: tail -n +2 means the first line is a title and the data starts at line two.
3. -c counts bytes, not lines
Besides counting lines, head and tail can count bytes with -c. head -c 20 lines.txt means take the first 20 bytes. The test output was line-01, line-02, and then a bare line with no newline. Why? Each line in lines.txt is line-01 plus a newline, 8 bytes in total. Twenty bytes is two whole lines (16 bytes) plus the first 4 letters of the next line, l, i, n, e. Once the byte count is reached it stops, whether the line is finished or not, so the rest of that line, e, -, 0, 3, was never printed. -c only knows byte counts, not lines: it is the right tool when you want "the first 20 bytes", and the wrong tool when you want "the first 3 lines".
The trap is worse with Chinese text, because one Chinese character takes 3 bytes. The first line of cn.txt is "第一行", three characters plus a newline, 10 bytes in total. head -c 3 cn.txt takes exactly the first character, 第, and the output is a clean single 第. But head -c 4 cn.txt takes 4 bytes and cuts the second character in half: the first 3 bytes are 第, and the 4th byte is the first byte of the next character, 二. The test looked at those 4 bytes in hexadecimal: head -c 4 cn.txt | od -An -tx1 printed e7 ac ac e4. The first three bytes, e7 ac ac, are 第, and the fourth byte, e4, is the start of 二, which needs all three bytes e4 ba 8c to be complete. Given only e4, it is a broken byte, and the terminal shows it as garbage or a question mark. Remember: -c counts bytes, not characters, so Chinese, Japanese, and emoji in multiple bytes are easy to cut in the middle. If you want lines, use -n; only use -c when you explicitly want to cut by bytes.
4. Several files at once: headers and -q
head and tail accept more than one file at a time. In the test, head -n 2 lines.txt cn.txt printed a line ==> lines.txt <==, then line-01 and line-02, then a blank line, then a line ==> cn.txt <==, then 第一行 and 第二行. Each file gets a header line in front of it, telling you which file the next few lines belong to. With a single file there is no header; with two or more files it is added automatically so you do not mix up which lines belong to whom.
To turn the headers off, use -q. head -q -n 2 lines.txt cn.txt printed just line-01, line-02, 第一行, 第二行, four lines together with no headers and no blank line. -q means quiet: contents only, no titles. When is that useful? When you pull the first lines out of several files and pass them on, for example head -q -n 1 *.log | sort to sort the first line of every log file — headers mixed in would only get in the way. When you just want to check what each file starts with, the headers are useful, so leave them on.
One more detail: if one of several files cannot be opened, head and tail report the error but do not stop. In the test, tail missing.txt printed tail: cannot open 'missing.txt' for reading: No such file or directory, with exit code 1. Errors go to standard error and normal output goes to standard output; the two are separate. In a script, add 2>&1 to the command if you want the errors collected too; if you only care about the contents, leave the errors alone.
5. tail -f: watch the tail while the file is still growing
The most useful thing tail does is -f: follow the end of the file, and print new lines as soon as the file grows. Watching logs during a deploy, waiting for a build to finish, keeping an eye on a file that is still being written — all of it uses this. -f means loop and keep reading: first print the tail, then keep waiting, and print new content the moment the file grows, until you press Ctrl+C or kill it. The test kept it under control with timeout: timeout 3 tail -n 0 -f watch.log. That means: follow starting from line 0, so none of the old content, only lines that grow after this point; timeout 3 kills it after 3 seconds. The output was line-03 and line-04 — two lines appended in the background one second apart, and tail -f printed every one of them as they arrived. After 3 seconds timeout killed tail, and the exit code was 124: 124 is timeout's convention, meaning "I killed the command; it did not exit on its own".
Before following, there is one more choice to make: how much old content to print first. tail -f with no -n prints the last 10 lines first and then follows, which fits when you have just picked up a log and want to see what happened recently before waiting for more. The test used tail -n 0 -f: -n 0 means no old content at all, only lines that grow from now on, which fits when you already know the old content and only care about what happens next. The difference is a single number: -n 50 -f prints 50 lines of old content first and then follows, -n 0 -f prints no old content and only waits for new lines. Pick whichever fits; picking the wrong one costs nothing more than a few lines seen or missed.
tail -f has one trap every beginner hits: it never exits on its own. If the file stops growing, it just waits, and it will wait forever unless you kill it. The test used timeout as a safety net on purpose; in day-to-day use, press Ctrl+C in an interactive terminal, or wrap it in timeout inside a script. Another way is --pid: tell tail which process to follow, and tail exits when that process dies. The test ran it like this: a background job slept 1 second, wrote build-done into build.log, and exited; the foreground ran timeout 5 tail --pid=$WPID -n 0 -f build.log, where $WPID is that background job's number. The output was a single line, build-done, with exit code 0 — the writer process died, so tail quit by itself without being killed. The manual gives a ready-made example: make >& makerr & tail --pid=$! -f makerr, where the build writes to a file and you watch it live, and tail stops on its own when the build finishes. Inside scripts, --pid is especially handy: wait for a background job to finish while watching its output in real time, all in one command.
6. After rotation: -f follows the descriptor, -F follows the name
tail -f has a subtler trap, and it shows up when logs rotate. Once a log file reaches a certain size, the system renames it — rot.log becomes rot.log.1, for example — and creates a fresh empty rot.log to keep writing into. Does tail -f still work then? The test says: it is still running, but it is following the wrong file. Here is how the test ran: timeout 4 tail -n 0 -f rot.log, with its output saved to follow-f.txt; after 1 second rot.log was renamed to rot.log.1, a new rot.log was created with new-1, and a second later new-2 was appended. The result: follow-f.txt was empty. Neither new-1 nor new-2 appeared. Why? Because -f follows the file descriptor by default — the already-open file itself. Once the file is renamed, tail still holds the old file's descriptor, it does not know the new file at all, and none of the lines written into the new file are visible to it. Even after the old file is deleted, it keeps waiting there and sees nothing.
The fix is to swap -f for -F. -F means --follow=name --retry: follow the file name instead of the descriptor, and keep retrying after the file is moved or deleted until it shows up again. Running the same steps with -F — timeout 4 tail -n 0 -F rot2.log, rename after 1 second, write new-1, then new-2 — gave a follow-F.txt with four lines: tail: 'rot2.log' has become inaccessible: No such file or directory, tail: 'rot2.log' has appeared; following new file, new-1, and new-2. tail first tells you the old file is gone, then tells you the new file has appeared and it is following the new one, and then prints new-1 and new-2. When you watch system logs or application logs, anything that can rotate should be watched with -F, not -f. The two commands differ by one letter, and once rotation happens the results are worlds apart: one loses all the new log lines, the other loses none.
7. head closed the pipe early: SIGPIPE 141
head has one more trap, and it comes from its habit of quitting as soon as it has the lines it wants. In seq 1 1000000 | head -n 2, head only wants the first two lines; it prints 1 and 2 and exits. But seq is still writing into the pipe, and when it writes and finds nobody reading at the other end, the system sends it a SIGPIPE signal and kills it. The test checked PIPESTATUS right away, and the output was PIPESTATUS=141 0. The 141 means 128 plus 13, and 13 is the signal number of SIGPIPE; the 0 after it is head's exit code, which exited normally. Together the two numbers tell the whole story: head took its two lines and left, and seq was killed by a signal because nobody was reading any more.
Most of the time you never notice, because a pipeline's exit code only reflects its last command by default. The whole pipeline seq 1 1000000 | head -n 2 exits 0, because head is 0. The fact that seq was killed is invisible in the exit code. When does it bite? In a script with set -o pipefail: with pipefail on, the pipeline's exit code becomes the exit code of the last command that failed, so seq's 141 surfaces and the whole pipeline becomes 141. The other case is when the upstream command matters: if the program generating the data is killed halfway, but the first two lines head printed look fine, you might think everything worked. Remember: head quitting early is normal behaviour, SIGPIPE is a normal mechanism, and when you see 141, first ask whether something downstream only took the first few lines and walked away.
8. The middle slice: combine head and tail
head only takes the front and tail only takes the back, so what about the middle? Combine the two: first use head to take everything up to the end of the slice you want, then use tail to take the last few lines of that. In the test, lines 11 to 15 came from head -n 15 lines.txt | tail -n 5, which printed line-11, line-12, line-13, line-14, line-15. head took the first 15 lines, tail took the last 5 of those 15, and what is left is exactly lines 11 to 15. For lines 31 to 40, write head -n 40 file | tail -n 10, changing the numbers by the same logic: head's number is the last line of the slice you want, and tail's number is how many lines the slice has.
This combination has another nice property: a short file never causes an error. In the test, lines.txt has only 20 lines, and head -n 100 lines.txt | wc -l printed 20: asked for 100 lines from a 20-line file, head simply handed over all 20, no more and no less, with exit code 0, no error and no blank filler lines. head and tail never fail because "the file is shorter than the number of lines you asked for", and that temperament is handy in scripts: you do not need to count the file's lines before deciding how many to take, just write head -n 100 and get however many lines there are. It works the other way too: tail -n 100 on a 20-line file prints all 20 lines. Taking the start, the end, and the middle — those three moves together can reach any slice of a file.
Two everyday uses follow from this. First, checking a data file's header: many comma-separated files put column names on the first line, so head -n 1 file shows the column names first, confirming the column order is what you expected before doing anything else. Second, skipping the header to process the data: tail -n +2 file prints from the second line onward, dropping the header line. Both use things already covered here — head -n 1 takes the first line, tail -n +2 starts at line two — nothing new, just the same tools in another place.
9. Checklist: reading files next time, follow this
First, to see just the start or the end, use the default 10 lines: head for the start, tail for the end. Second, change the line count with -n: with no sign it means "take this many lines", head -n -3 means "everything except the last 3 lines", and tail -n +6 means "from line 6 onward". Third, -c cuts by bytes, and remember it counts bytes, not characters — Chinese text gets cut in half. Fourth, with several files the headers tell you which lines belong to which file; add -q when you do not want them. Fifth, use tail -f to watch a file that is still growing, and remember it never exits on its own: Ctrl+C interactively, or timeout or --pid in a script. Sixth, when logs rotate, use tail -F: it follows the name, not the descriptor, so no new log line is lost. Seventh, head quitting after a few lines kills the upstream writer with SIGPIPE, which is normal; PIPESTATUS shows it as 141. Eighth, for a slice in the middle, use head to take everything up to the end of the slice and tail to take its last lines; a short file never errors.
Finally, the boundary. This guide covers only the head and tail pair: taking the start, taking the end, and keeping up with change. Searching inside logs, replacing text, and counting by column belong to grep, sed, and awk in the text-processing guide, and are not repeated here. Querying the system log is journalctl's job; tail -f watches plain text files and never touches the system log store. head and tail never change a file — they only read it out for a look, so look first with them to confirm a file is the one you mean, and only then change it. Test record: every step above was run for real on GNU coreutils 9.4, and the full output is in demo279_headtail.sh and demo279_headtail_transcript.txt (130 lines). No matter how big a file is, what matters is its two ends: what happened at the start, where it stopped at the end, and the middle taken on demand.
Comments (0)
Sources
Related articles
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.