tail Study Notes

tail

is the counterpart to head

  • head beginning of file
  • tail end of file

But unlike head, tail has a very important extra capability

Default behavior:

tail file.txt

prints the last 10 lines of the file


-n

Last 20 lines

tail -n 20 file.txt

-c

Last 100 bytes

tail -c 100 file.txt

Positive + numbers

tail -n +5

start FROM line 5 onward

Example:

1
2
3
4
5
6
7

Command:

tail -n +5 file.txt

Output:

5
6
7

Same with bytes:

tail -c +2 file.txt

start from byte 2


-f, --follow

This is the real power of tail

tail -f logfile.log

tail stays running, waits for new data, prints new lines live


--retry

Indefinitely try to open the specified file. This option is useful mainly when following (and otherwise issues a warning).

When following by file descriptor, this option only affects the initial open of the file, as after a successful open, tail will start following the file descriptor.

When following by name (with --follow=name), tail infinitely retries to re-open the given files until killed.

Without this option, when tail encounters a file that doesn’t exist or is otherwise inaccessible, it reports that fact and never checks it again.


-F

This option is the same as --follow=name --retry. That is, tail will attempt to reopen a file when it is removed. Should this fail, tail will keep trying until it becomes accessible again.