How to Use the Tail Command in Linux

A utility command in Linux to view a portion of file content. It can be used to view the beginning, the end or specific line of the file.

General syntax for tail command:

$ tail [OPTION...] [FILE...]
Display the exact number of lines
tail -n [number] [file]

Using this option allows you to get the tail command to produce output that will display a certain number of lines in a file.

Example: Displaying the last three lines of the example.txt file.

display number of lines
Display lines starting from a specific line number
tail +[number] [file]

The command with the + sign outputs data starting from the specified line number.

Example: Using the tail command to print all lines after the first four lines of the example.txt file.

display starting from number
Display multiple files
tail [file1] [file2]

Use this command to display the data of multiple files at the same time.

Example: Displaying both files new1.txt and new2.txt using the tail command.

display multiple files
Output a certain number of bytes
tail -c [bytes] [file]

To display a specific number of bytes in a text file, use the -c option.

Example: This command displays the last 36 bytes of the example.txt file.

ouput number of bytes
Use multiple commands at once
tail [file] | [other_command]

Use the tail command with pipes | to use it in conjunction with another command.

Example: Using the sort command with the tail command to display and sort the contents of the example.txt file alphabetically.

use multiple command at once
Monitor files in real-time
tail -f [file]

The -f option is used to track file changes. When new log entries are added to the log file, it updates the display in the terminal window.

Example: Displaying the last changes of the auth.log file in the terminal.

monitor files realtime