Linux logo

What is Linux tail command?

Ever wondered about the Linux superhero that effortlessly reveals the tail-end secrets of files? Enter the mighty tail command! By default, it unveils the last 10 lines of files or even piped data. But here’s the kicker – it’s not just a static observer; it’s your real-time file change vigilante.

This command isn’t your average Joe; it’s a dynamic troubleshooter, a data detective. Its prime mission? To keep an eye on logs and ever-evolving files, working hand in hand with sidekicks like grep for some serious file analysis.

In this guide, we’re taking you into the sleek world of the tail command. No jargon, just cool examples to unlock its secrets. Let’s make understanding the tail command as smooth as a superhero landing!

Difference between the tail and head commands?

As their names indicate, the head command will output the first part of the document, whereas the tail command will print the final portion of the file. Both commands write the result to standard output.

Syntax for using Linux tail command
$ tail [OPTION]… [FILE1]..[FILE2]…
An example of the tail command

Basic usage is extremely easy – all you need to do is to pass the name of the file. For example:

$ tail file1

This will display the last ten lines of the specified file. The ten lines is the default.

How To Display a Particular Number of Lines

Use the -n (–lines) option to specify the number of lines to display:

$ tail -n <NUMBER> filename.txt

You may also omit the letter -n and use only the hyphen (-) and the number (without a space between them).

To display the last 20 lines of a file named test.txt you’d use:

$ tail -n 20 test.txt
How to Display a Particular Number of Bytes

To show a particular number of bytes use the -c (–bytes) option.

$ tail -c <NUMBER> filename.txt

For example to display the last 120 bytes of data in the file named test.txt you’d use:

$ tail -c 120 test.txt
Multiple files illustration

We can open a number of files from last simultaneously by executing the tail command. To display the multiple documents, provide the file names as input. It will display the last ten lines of files that are specified.

$ tail -n <number of lines> <file1> <file2> <file3>

For example:

$ tail test1.txt test2.txt

For the last 20 lines for each file:

$ tail -n 20 test1.txt test2.txt
Track a file for changes

To monitor a file for modifications, the ‘-f’ option is used. Here, the ‘-f’ stands for the next. It’s helpful for monitoring log files. Execute the below command:

$ tail -f test.txt

The above command will track the document test.txt’. To exit from tracking, press the “CTRL+C” keys.

Further Reading:  A Little About Vim and How to Search in it
Using the head and the tail together example

As mentioned earlier, the head command can give us the first part of a document, while the tail command can output the final part of the input file. But what if we would like to find some part in the midst of a file?

To fix this issue, we can combine the two simple commands.

Let us say we want to get in the 21st to the 26th line from an input file:

$ tail -n +21 test.txt | head -n 5

Best Practices for Using the Linux tail Command

  1. Basic Usage:
    • Utilize the basic syntax: $ tail [OPTION]… [FILE1]..[FILE2]…
    • Example: $ tail file1
  2. Display a Specific Number of Lines:
    • Use the -n (or --lines) option to specify the number of lines to display.
    • Example: $ tail -n 20 test.txt
  3. Display a Specific Number of Bytes:
    • Use the -c (or --bytes) option to show a particular number of bytes.
    • Example: $ tail -c 120 test.txt
  4. Working with Multiple Files:
    • Open multiple files simultaneously by providing file names as input.
    • Example: $ tail test1.txt test2.txt
    • For the last 20 lines for each file: $ tail -n 20 test1.txt test2.txt
  5. Tracking File Changes in Real-Time:
    • Monitor a file for modifications using the -f option (follow).
    • Example: $ tail -f test.txt
    • Press “CTRL+C” to exit from tracking.
  6. Combining head and tail Commands:
    • Combine head and tail for extracting a specific range of lines from the middle of a file.
    • Example: $ tail -n +21 test.txt | head -n 5
  7. Understanding the Difference with head Command:
    • Differentiate between tail and head commands; tail shows the last part, while head displays the first part of a file.
  8. Efficient Log Monitoring:
    • Leverage tail for efficient monitoring of logs and files that change over time.
    • Combine with tools like grep for advanced log analysis.
  9. Customizing Output with Options:
    • Explore additional options to customize the output based on specific requirements.
    • Experiment with combinations of -n and -c for more tailored results.
  10. Exiting Gracefully:
    • When using the -f option for tracking changes, exit gracefully by pressing “CTRL+C.”
Further Reading:  What is date Command in Linux?

 

Troubleshooting and FAQs for the Linux tail Command

  1. Difference Between tail and head Commands:
    • Q: What distinguishes tail from head?
      • A: tail displays the final portion of a file, while head shows the first part. Both write to standard output.
  2. Common Syntax for tail Command:
    • Q: What is the general syntax for using tail?
      • A: $ tail [OPTION]… [FILE1]..[FILE2]…
  3. Basic Usage:
    • Q: How do I use tail for basic file display?
      • A: Example: $ tail file1 (displays the last ten lines by default).
  4. Display a Specific Number of Lines:
    • Q: How can I show a specific number of lines?
      • A: Use the -n (or --lines) option, e.g., $ tail -n 20 test.txt.
  5. Display a Specific Number of Bytes:
    • Q: Can I display a specific number of bytes?
      • A: Yes, use the -c (or --bytes) option, like $ tail -c 120 test.txt.
  6. Working with Multiple Files:
    • Q: How can I open multiple files simultaneously with tail?
      • A: Provide file names as input, e.g., $ tail test1.txt test2.txt.
  7. Tracking File Changes in Real-Time:
    • Q: How do I monitor a file for real-time changes?
      • A: Use the -f option, e.g., $ tail -f test.txt. Exit with “CTRL+C.”
  8. Troubleshooting Tracking Issues:
    • Q: What if real-time tracking isn’t working?
      • A: Ensure the file exists, permissions are correct, and try restarting the tracking.
  9. Combining head and tail:
    • Q: Can I use head and tail together?
      • A: Yes, example: $ tail -n +21 test.txt | head -n 5 extracts lines 21 to 26.
  10. Optimizing Log Monitoring:
    • Q: How can I optimize log monitoring with tail?
      • A: Combine with tools like grep for advanced log analysis and filtering.
  11. Exiting Real-Time Tracking:
    • Q: How do I stop real-time tracking?
      • A: Press “CTRL+C” to exit when using the -f option.
  12. File Unavailability in Real-Time:
    • Q: What if the file is not available during tracking?
      • A: Ensure the file is created or accessible, and permissions allow tracking.
  13. Checking File Changes Midway:
    • Q: How can I extract lines from the middle of a file?
      • A: Combine head and tail as demonstrated: $ tail -n +21 test.txt | head -n 5.

 

Concluding Linux tail Command

In this tutorial, we went into the fundamental principles of the tail command, a versatile tool in the Linux command-line arsenal. The tail command excels at displaying the tail end of files, making it particularly useful for monitoring log files, tracking changes in real-time, and extracting specific sections of data.

Further Reading:  Linux CD Command: Understand How it Works

Key Takeaways:

  1. Default Display:
    • The tail command, when used without options, displays the last ten lines of a file by default.
  2. Real-Time Tracking:
    • A notable capability of tail is real-time tracking (-f option), enabling users to monitor file changes dynamically, such as in log files.
  3. Numeric Options:
    • Users can customize the number of lines displayed using the -n option, and similarly, the -c option allows the display of a specific number of bytes.
  4. Working with Multiple Files:
    • tail supports the simultaneous display of the last lines from multiple files, offering efficiency in monitoring various logs concurrently.
  5. Combining with Other Commands:
    • Combining tail with other commands, such as head, allows users to extract specific sections from the middle of a file. This enhances flexibility in data extraction.
  6. Troubleshooting and Optimization:
    • The tutorial provided troubleshooting tips for issues related to real-time tracking and emphasized the optimization of log monitoring using tools like grep in conjunction with tail.

Further Exploration:

  • Users are encouraged to explore additional options and advanced use cases to maximize the utility of the tail command based on specific system administration or log analysis needs.

In conclusion, the tail command, with its simplicity and powerful features, stands as an invaluable tool for Linux users, enabling efficient file monitoring and data extraction in various scenarios. Mastering the tail command contributes to a robust skill set for effective system administration and log analysis tasks.