tail command tutorial in linux/unix with examples and use cases

linux tail command — output the last part of files.

Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.

The linux tail command corresponds to the linux head command.

syntax

tail [OPTION]... [FILE]...

options

  • -c, –bytes=[+]NUM
    output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file
  • -f
    -f option causes tail to not stop when end of file is reached, but rather to wait for additional data to be appended to the input.
  • -n, –lines=[+]NUM
    output the last NUM lines.
  • –pid=PID
    with -f, terminate after process ID, PID dies
  • -q, –quiet, –silent
    never output headers giving file names
  • –retry
    keep trying to open a file if it is inaccessible
  • -s, –sleep-interval=N
    with -f, sleep for approximately N seconds (default 1.0) between iterations; with inotify and –pid=P, check process P at least once every N seconds
  • -v, –verbose
    always output headers giving file names
  • -z, –zero-terminated
    line delimiter is NUL, not newline

linux tail examples

Display the last 10 lines of the file

In the following example, we will use tail to display the last 10 lines of the file.

➜  ~ tail /var/log/dmesg

Display the last 3 lines of file content

➜  ~ tail -3 /var/log/dmesg

Tail skip first N lines

from line 20 to the end of the file:

➜  ~ tail +20 /var/log/dmesg

To monitor the write growth of log files

In the following example, we will use the tail command -f option to monitor file changes in real time; As the file content increases, the output is in real time.

➜  ~ tail -f /var/log/dmesg

use tail with pipes(|)

Display the last 2 lines of the first 10 lines of the file dmesg

➜  ~ head -10 /var/log/dmesg | tail -2 

Add a Comment

Your email address will not be published. Required fields are marked *