← Full reference

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

OptionWhat it does
-n 5 (-5)first 5 lines
-n -5everything except the last 5 lines
-c 100first 100 bytes; -c -100 — everything except the last 100 bytes
-qdon't print ==> file <== headers with several files
-vprint the header even for a single file
-zlines are separated by a null byte, not \n

tail options

OptionWhat it does
-n 5 (-5)last 5 lines
-n +5everything starting from line 5 (skip the first 4)
-c 100 / -c +100last 100 bytes / everything starting from byte 100
-q / -vnever / always print file headers
-zlines 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

OptionWhat it does
-fdon't exit after output, print new lines as they are appended. Follows the open file (--follow=descriptor)
--follow=namefollow the name: if the file is renamed or deleted and recreated, reopen it
--retrykeep trying to open the file if it doesn't exist yet or is inaccessible
-Fsame as --follow=name --retry — the right choice for rotated logs
--pid=PIDwith -f: exit when process PID ends
-s Nwith -f: check the file every N seconds (default 1; rarely needed with inotify)
--max-unchanged-stats=Nwith --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 +5 means "starting from line 5", not "the last 5". The plus sign changes the meaning; for head the minus is special: head -n -5.
  • tail -f holds on to the open file. If the log is renamed during rotation (app.log → app.log.1) and a new one is created, tail -f keeps reading the old file and won't see new lines. For rotated logs use tail -F. If the file is just truncated (> app.log), GNU tail notices and prints file truncated.
  • tail -f never exits on its own. In a script it hangs; limit it with timeout or --pid.
  • Buffering in a pipeline. In tail -f … | grep … | … grep buffers its output, and lines show up with a delay. grep --line-buffered helps (for sed — -u, for other programs — stdbuf -oL).
  • Numbering starts at one. tail -n +1 prints the whole file, tail -n +2 skips the first line.
  • Without a trailing \n the last "line" still counts as a line: printf 'a\nb' | tail -n 1 prints b (without a newline).
  • head in a pipeline closes the pipe early. The command on the left may get SIGPIPE and exit with an error — with set -o pipefail that makes the whole pipeline fail.

See also

sed · grep · wc · logrotate