Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. head / tail

head / tail

Since: All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

The head command displays the beginning of a file, and tail displays the end. In particular, tail -f is commonly used to monitor log files in real time.

Syntax

head [option] [file...]
tail [option] [file...]

Options

OptionDescription
head fileDisplays the first 10 lines of a file.
head -n N fileDisplays the first N lines.
head -c N fileDisplays the first N bytes.
head -n -N fileDisplays all lines except the last N lines.
tail fileDisplays the last 10 lines of a file.
tail -n N fileDisplays the last N lines.
tail -c N fileDisplays the last N bytes.
tail -f fileMonitors the end of a file and displays new content in real time as it is appended.
tail -F fileSame as -f, but continues tracking even if the file is deleted and recreated.
tail -n +N fileDisplays lines starting from line N (skips the first N-1 lines).

Sample Code

The following file is used in the examples below.

~/project/data.csv
id,name,score
1,webmaster,85
2,appserver,72
3,user3,91
4,sysadmin,68
5,devuser,95
6,webmaster,80
7,appserver,88

Display the first 3 lines of the file.

head -n 3 data.csv
id,name,score
1,webmaster,85
2,appserver,72

Display the last 3 lines of the file.

tail -n 3 data.csv
5,devuser,95
6,webmaster,80
7,appserver,88

Skip the CSV header (first line) and retrieve all rows from line 2 onward.

tail -n +2 data.csv
1,webmaster,85
2,appserver,72
3,user3,91
4,sysadmin,68
5,devuser,95
6,webmaster,80
7,appserver,88

Check the first 3 lines of /etc/passwd.

head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

Monitor a log file in real time (press Ctrl+C to stop).

tail -f /var/log/nginx/access.log

Continue tracking a log file even after log rotation.

tail -F /var/log/app.log

Combine with a pipe to display only the latest 10 error log entries.

grep "ERROR" app.log | tail -n 10

Common Mistakes

Common Mistake 1: Forgetting -n and getting the wrong number of lines

Without -n, head and tail default to 10 lines. Passing a number without -n is interpreted as a flag on older systems and may cause an error.

head 5 file.txt
head: cannot open '5' for reading: No such file or directory

Always use -n to specify the line count explicitly.

head -n 5 file.txt

Common Mistake 2: tail -f does not follow log rotation

tail -f tracks the file descriptor. When a log file is rotated (renamed/replaced), tail -f keeps watching the old file and stops receiving new entries.

tail -f /var/log/app.log
(log rotation creates a new file, but tail -f keeps watching the old one)

Use tail -F (capital F) to follow by filename and automatically reopen the file after rotation.

tail -F /var/log/app.log

Notes

tail -f is a command you will use almost daily for server log monitoring. To monitor multiple files at once, you can specify them together: $ tail -f file1 file2. For more powerful log monitoring, tools such as multitail and lnav are also available.

To browse an entire file interactively, use less. See also cat for displaying full file contents.

If you find any errors or copyright issues, please .