Skip to content

Linux tail command

The Linux tail command is a useful tool for viewing the end of files or streams in real-time. It is commonly used to monitor log files and track changes as they occur. With tail, you can easily follow the newest entries in a file without having to open the entire document. This command is essential for system administrators and developers who need to stay updated on the latest information within a file. By using various options and flags, you can customize the output to suit your specific needs, making tail a versatile and powerful tool in the Linux environment.

tail Syntax:

Terminal window
tail [option] [file]

Options:

OptionDescription
-n NUMOutput the last NUM lines
-fOutput appended data as file grows
-qNever output headers giving file names
-vAlways output headers giving file names

Parameters:

ParameterDescription
fileThe file to display

tail Usage:

Display the last 10 lines of a file

Terminal window
tail filename.txt

This command will display the last 10 lines of the file named “filename.txt”.

Continuously display new lines added to a file

Terminal window
tail -f log.txt

Using the -f option with tail allows for continuous monitoring and display of new lines added to the file “log.txt”.

Display the last 20 lines of a file

Terminal window
tail -n 20 example.log

By specifying the -n option followed by the number of lines (in this case 20), tail will display the last 20 lines of the file named “example.log”.

Display and update the last 5 lines of a file every 2 seconds

Terminal window
tail -n 5 -f -s 2 data.txt

This command will continuously display and update the last 5 lines of the file “data.txt” every 2 seconds.

How do I use tail in Linux?

To use the tail command in Linux, execute the following command:

Terminal window
tail file.txt

What option can I use to show a specific number of lines from the end of a file?

You can use the -n or --lines option followed by the number of lines to display. For example:

Terminal window
tail -n 15 file.txt

How can I continuously monitor a file for new lines being added?

To continuously monitor a file for new lines, use the -f or --follow option. This will keep the file open and show any new lines added in real-time. For example:

Terminal window
tail -f file.txt

How can I output lines from the beginning of a file instead of the end?

You can use the -r or --rev option to display lines from the beginning of a file, starting with the specified number of lines. For example:

Terminal window
tail -r -n 5 file.txt

How do I display lines from the end of a file along with line numbers?

You can use the -n or --lines option along with the -n option to display line numbers along with the lines from the end of the file. For example:

Terminal window
tail -n 10 -n file.txt

How can I ignore a specific number of lines from the end of a file?

You can use the + symbol followed by the number of lines to ignore from the end of a file. For example, to ignore the last 3 lines:

Terminal window
tail +4 file.txt

How do I display the last part of multiple files at once?

To display the last part of multiple files concurrently, you can specify the files as arguments after the options. For example:

Terminal window
tail -n 5 file1.txt file2.txt file3.txt

Applications of the tail command

  • Displaying the last n lines of a file
  • Following a file in real-time as it grows
  • Viewing the end of log files
  • Monitoring log files for changes
  • Extracting the last few lines of a file