The tail command in Linux is a powerful tool used for displaying the end of a file. By default, it displays the last 10 lines of a file, but this can be modified by specifying a different number of lines to display. The tail command is often used to monitor log files, debug applications, or view the contents of a file in real time.

Syntax

The basic syntax for using the tail command is as follows:

where options are any optional flags or parameters that modify the behavior of the tail command, and the file is the name of the file to display.

Some common options used with the tail command include:

  • -n: Specifies the number of lines to display. For example, to display the last 20 lines of a file, you would use the following command: tail -n 20 [file]
  • -c: Specifies the number of characters to display. For example, to display the last 100 characters of a file, you would use the following command: tail -c 100 [file]
  • -f: Follow mode, which continually displays the contents of a file as they are added. This is useful for monitoring log files in real time.
  • -q: Quiet mode, which suppresses the output of the file names. This is useful when you are processing multiple files and only want to see the output, without the file names.

tail Command Examples

Here are some step-by-step examples of using the tail command:

  1. To display the last 10 lines of a file named file.txt, simply type:
    tail file.txt 
    
  2. To display the last 20 lines of a file named file.txt, use the following command:
    tail -n 20 file.txt
    
  3. To display the last 100 characters of a file named file.txt, use the following command:
    tail -c 100 file.txt
    
  4. To monitor a log file named log.txt in real time, use the following command:
    tail -f log.txt
    
  5. To display the last 10 lines of multiple files, named file1.txt and file2.txt, use the following command:
    tail file1.txt file2.txt
    
  6. To display the last 20 lines of multiple files, named file1.txt and file2.txt, without the file names, use the following command:
    tail -n 20 -q file1.txt file2.txt
    

Conclusion

In conclusion, the tail command is an essential tool for any Linux user. Whether you need to monitor log files, debug applications, or simply view the contents of a file, the tail command provides a convenient and efficient way to do so. By using the options available with the tail command, you can easily customize and automate the process, making it an even more valuable tool in your Linux toolkit.