How Does the “tail” Command’s “-f” Parameter Work?

The “tail” built-in command line tool prints the last few lines of the input data files. The “-f” parameter is used to follow the file as it grows. When the “-f” parameter is used, the tail does not exit after displaying the last few lines of the file. Instead, it continues to monitor the file for any new lines added to it and displays them as they are added. The main objective of the tail command is to figure out the error messages and monitor the file changes in real-time. 

This post explains the main objective and working of the “tail” command parameter “-f(follow)”.

  • Monitor Text Files For Changes
  • Monitor Logs For Updates 

How Does the “tail” command’s “-f” Parameter Work?

The “tail” command offers the “-f(follow)” parameter that follows the changes in the output of a file continuously in real-time. It is mainly used by the system administrator for monitoring the text and log files. It also shows the addition of new lines in a specific file by accessing it through another terminal.

Syntax

The working of the “tail -f” command requires the filename whose content needs to be monitored in this format:

$ tail -f filename

If no filename is passed with the “tail -f” command then “descriptor” is used automatically by default and this process is called the “live tail”. 

Example 1: Monitor Text Files For Changes

Let’s see the “tail -f” working practically with the help of an example to monitor the file changes.

Create a File

Create a text file named “Note.txt” with the help of the simplest built-in “cat” command:

$ cat > Note.txt

Monitor the File

Now open another second terminal and execute the “tail -f” command with the “Note.txt” filename to monitor the changes and saves them:

$ tail -f Note.txt

At that time the “tail -f” command is monitoring the “Note.txt” file. Now if the user adds any content to the “Note.txt” file the “-f” parameter of the tail will follow it like this as shown in the following gif:

Press the “Ctrl+C” shortcut key to exit the monitoring process. 

Example 2: Monitor Logs For Updates 

The logs act as a piece of information on very action performed on the Linux system such as mail, cron services, kern, daemon, and much more.  The “tail -f” command continuously monitors the logs and shows the new updates added to it time by time. when the system administration.

In Linux, logs are stored in the “/var/log/message” directory except from Ubuntu. It stores the logs in the “syslog” directory. To monitor that “syslog” directory use the “tail -f” command in this way:

$ tail -f /var/log/messages

The output contains the last few lines of the “var/log/syslog” directory of Ubuntu operating system.

Conclusion

In Linux, the “tail” command with the “-f” parameter follows the addition or updation of the new lines into the particular file. It monitors the input data file continuously until the monitoring operation is stopped. To stop the monitoring operation, use the “Ctrl+C” key from the keyboard.

This post has demonstrated the purpose and the working of the “tail” command’s “-f” parameter.