Linux tail Command Tutorial with Examples

Linux provides the tail command in order to read specified files. The default behavior of the tail command is reading the specified files ends with the specified lines. By default, the tail command reads the last 10 lines of the specified files.

tail Command Syntax

The tail command has the following syntax where generally a single file is specified to read from its end. But also multiple files can be specified too which is optional.

tail OPTION FILE1 FILE2 ...
  • OPTION is the option to set different behaivours for the tail command.
  • FILE1 is the file to read from its end.
  • FILE2 is optional and generally not used.

Print Last 10 Lines of File

By default the tail command prints the provided file’s last 10 lines. There is no need for an extra parameter for the tail command. In the following example, we print the last 10 lines of the “/var/log/auth.log”.

$ tail /var/log/auth.log
Print Last 10 Lines of File

Print Last Specified Number Of Lines

The number of lines we want to print can be specified with the -n option. The number of lines is provided after the -n option. In the following example, we print the last 3 lines of the “/var/log/auth.log”.

$ tail -n 3 /var/log/auth.log
Print Last Specified Number Of Lines

Print Last Specified Number Of Bytes

The tail command can be also used to print a specified number of bytes from the specified file. The -c option is used to specify by count. In the following example, we print the last 64 bytes from “/var/log/auth.log”.

$ tail -c 64 /var/log/auth.log

Print Last Lines Interactively

One of the most popular features of the tail command is printing newly added lines in an interactive way. The tail command continuously monitors the specified file and prints newly added lines automatically. This is very useful to monitor logs and changes in a file without any extra effort. The -f option is used

$ tail -f /var/log/auth.log

Print Last Lines with File Name

Generally, the tail command is used with a single file and there is no need to print the file name for the output. If we use multiple files with tail command and require to print the file names the -v option can be used.

$ tail -v /var/log/*
Print Last Lines with File Name

Print Multiple Files

The tail command can be used to print multiple files in a single execution. The file names can be provided to the tail command.

$ tail /var/log/auth.log users.log

Print Version Information

Even the tail command is very stable and less changing command the version information about the tail command can be printed with the --version option like below.

$ tail --version

Print Help Information

Basic help information about the tail command can be printed with the --help option like below.

$ tail -h
Print Help Information
See also  Read File Line By Line In Bash

Leave a Comment