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.

  1. Home
  2. Bash Dictionary
  3. head / tail

head / tail

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,Alice,85
2,Bob,72
3,Charlie,91
4,Diana,68
5,Eve,95
6,Frank,80
7,Grace,88

Display the first 3 lines of the file.

head -n 3 data.csv
id,name,score
1,Alice,85
2,Bob,72

Display the last 3 lines of the file.

tail -n 3 data.csv
5,Eve,95
6,Frank,80
7,Grace,88

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

tail -n +2 data.csv
1,Alice,85
2,Bob,72
3,Charlie,91
4,Diana,68
5,Eve,95
6,Frank,80
7,Grace,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

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 .