head and tail
First and last lines of a file, slicing by line numbers and following a growing log with tail -f
head prints the beginning of a file, tail the end. 10 lines by default. tail -f doesn't exit but keeps printing new lines as they are appended to the file — this is how you watch logs in real time.
Syntax
head [-n N] [file…]
tail [-n N] [file…]
tail -f | -F [-n N] file…
Without files (or with the file -) both commands read stdin.
head options
| Option | What it does |
|---|---|
-n 5 (-5) | first 5 lines |
-n -5 | everything except the last 5 lines |
-c 100 | first 100 bytes; -c -100 — everything except the last 100 bytes |
-q | don't print ==> file <== headers with several files |
-v | print the header even for a single file |
-z | lines are separated by a null byte, not \n |
tail options
| Option | What it does |
|---|---|
-n 5 (-5) | last 5 lines |
-n +5 | everything starting from line 5 (skip the first 4) |
-c 100 / -c +100 | last 100 bytes / everything starting from byte 100 |
-q / -v | never / always print file headers |
-z | lines are separated by a null byte |
Numbers in -n and -c accept suffixes: K = 1024, M = 1024², kB = 1000 and so on (head -c 1K).
Following a file
| Option | What it does |
|---|---|
-f | don't exit after output, print new lines as they are appended. Follows the open file (--follow=descriptor) |
--follow=name | follow the name: if the file is renamed or deleted and recreated, reopen it |
--retry | keep trying to open the file if it doesn't exist yet or is inaccessible |
-F | same as --follow=name --retry — the right choice for rotated logs |
--pid=PID | with -f: exit when process PID ends |
-s N | with -f: check the file every N seconds (default 1; rarely needed with inotify) |
--max-unchanged-stats=N | with --follow=name: reopen the file if its size hasn't changed for N checks (default 5) |
When following several files, tail prints a ==> file <== header each time it switches to another file. It reports truncation and replacement of a file on stderr: file truncated, has been replaced; following new file.
Examples
seq 100 > nums.txt
# first and last 3 lines
head -n 3 nums.txt
tail -n 3 nums.txt
# skip the header line (e.g. of a CSV or ps output)
tail -n +2 nums.txt | head -n 3
ps aux | tail -n +2 | wc -l
# lines 20 through 25
head -n 25 nums.txt | tail -n +20
sed -n '20,25p' nums.txt
# everything except the last 95 lines
head -n -95 nums.txt
# first 16 bytes of a file — look at the signature
head -c 16 /bin/ls | od -c
# the beginning of each of several files, with and without headers
head -n 1 /etc/passwd /etc/group
head -q -n 1 /etc/passwd /etc/group
# follow a log: show the last 20 lines and wait for new ones (Ctrl+C to quit)
tail -n 20 -f app.log
# follow by name — survives log rotation and the file appearing later
tail -F app.log
# follow several logs at once
tail -F app.log error.log
# follow and filter on the fly
tail -F app.log | grep --line-buffered ERROR
# follow for a limited time
timeout 10 tail -f app.log
# follow while a background process runs, and exit together with it
sleep 5 &
tail -f app.log --pid=$!
Pitfalls
tail -n +5means "starting from line 5", not "the last 5". The plus sign changes the meaning; forheadthe minus is special:head -n -5.tail -fholds on to the open file. If the log is renamed during rotation (app.log→app.log.1) and a new one is created,tail -fkeeps reading the old file and won't see new lines. For rotated logs usetail -F. If the file is just truncated (> app.log), GNU tail notices and printsfile truncated.tail -fnever exits on its own. In a script it hangs; limit it withtimeoutor--pid.- Buffering in a pipeline. In
tail -f … | grep … | …grep buffers its output, and lines show up with a delay.grep --line-bufferedhelps (for sed —-u, for other programs —stdbuf -oL). - Numbering starts at one.
tail -n +1prints the whole file,tail -n +2skips the first line. - Without a trailing
\nthe last "line" still counts as a line:printf 'a\nb' | tail -n 1printsb(without a newline). headin a pipeline closes the pipe early. The command on the left may get SIGPIPE and exit with an error — withset -o pipefailthat makes the whole pipeline fail.