tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last 10 lines of a file, but this can be modified with command line options. The tail command is often used to view the end of large log files or to monitor the output of a running process.

The basic syntax of the tail command is:

tail [options] [file]

When executed without any options, the tail command will display the last 10 lines of the specified file. For example, the following command will display the last 10 lines of the file "example.txt":

tail example.txt

The most commonly used option for the tail command is the "-n" option, which allows you to specify the number of lines to display. For example, the following command will display the last 20 lines of the file "example.txt":

tail -n 20 example.txt

Another useful option for the tail command is the "-f" option, which stands for "follow." This option causes the tail command to continuously display the last few lines of a file as they are added, similar to the "tail -f" command in Linux. This can be useful for monitoring log files or other files that are being updated in real-time.

tail -f example.txt

The tail command also has an option "-c" for displaying the last 'n' bytes of a file. This option is useful when you want to see the last bytes of a file, for example, last 100 bytes of a file.

tail -c 100 example.txt

Additionally, the tail command can also be used with pipes, which allows the output of one command to be passed as input to another command. For example, the following command will display the last 10 lines of the output of the "df" command, which shows the disk usage on the system:

df | tail

The tail command can also be used with the "grep" command to filter the output. For example, the following command will display the last 10 lines of the file "example.txt" that contain the word "error":

tail example.txt | grep error

The tail command can also be used in shell scripts and automation tasks to capture the last few lines of a file, as well as to monitor the output of a running process.

The tail command is a versatile command that can be used in a variety of ways, making it a valuable tool for system administrators, developers, and other users. Some of the other key features and uses of the tail command include:

  • By default, the tail command reads the entire file and then displays the last 10 lines. But with the option -n +m, it can be used to display the lines starting from the mth line of the file.
    • tail -n +m example.txt
  • The tail command can also be used to monitor multiple files at the same time using the option -q and -f. The -q option tells tail to suppress the output of the file name and the -f option makes tail to follow the files for changes.
    • tail -qf file1 file2
  • By using the option -v, tail command can be used to display the lines that are not common between two files.
    • tail -v file1 file2
  • The tail command can also be used to display the output of a command in real-time. For example, the following command will display the output of the "ping" command in real-time:
    • ping google.com | tail -f
  • The tail command can also be used to display the output of a command that is running in the background. For example, the following command will start the "command1" in the background and display the output in real-time:
    • command1 & tail -f logfile.txt
  • The tail command can also be used to display the output of multiple files at the same time by using the option -n and -f together. For example, the following command will display the last 5 lines of the file1, file2 and file3 in real-time:
    • tail -n 5 -f file1 file2 file3
  • The tail command can also be used to display the output of a command in a specific format. For example, the following command will display the output of the "ls" command in a long format:
    • ls -l | tail

In summary, the tail command is a powerful and versatile command that can be used in a variety of ways. It can be used to display the last few lines of a file, monitor the output of a running process, display the output of multiple files at the same time, and display the output of a command in a specific format. With the various options and combination of other commands, tail command is a very handy tool for system administrators, developers, and other users to monitor and troubleshoot the system.