mooc-course.com is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

Show Last Lines of a File in Linux | Linux Tail Command with Examples

The Linux tail command is a powerful utility that allows users to view the end of a text file or monitor real-time changes. Whether you’re a system administrator, developer, or IT professional, understanding how to use the tail command effectively can significantly enhance your productivity when working with log files and other text-based data.

What is the Linux Tail Command?

The tail command is primarily used to display the last part of a file. By default, it outputs the last 10 lines of the specified file to the standard output. However, it’s highly versatile and can be customized to suit various needs.

Basic Syntax of the Tail Command

The basic syntax of the tail command is as follows:

tail [OPTION]... [FILE]...

If no FILE is specified, or when FILE is -, the command reads from standard input.

Key Options of the Tail Command

Let’s explore some of the most commonly used options with the tail command:

  1. Specifying the Number of Lines (-n)

    The -n option allows you to specify the number of lines you want to display:

    tail -n 20 filename.txt

    This command will display the last 20 lines of the file.

  2. Following File Changes in Real-Time (-f)

    The -f option is one of the most powerful features of the tail command. It allows you to monitor a file in real-time, displaying new lines as they are added to the file:

    tail -f /var/log/syslog

    This command is particularly useful for monitoring log files.

  3. Specifying the Number of Bytes (-c)

    The -c option lets you specify the number of bytes to display from the end of the file:

    tail -c 100 filename.txt

    This command will display the last 100 bytes of the file.

  4. Displaying Multiple Files

    You can use the tail command with multiple files:

    tail file1.txt file2.txt

    This will display the last 10 lines of each file, with a header indicating the file name.

  5. Quiet Mode (-q)

    The -q option suppresses the headers when displaying multiple files:

    tail -q file1.txt file2.txt

Practical Examples of Using the Tail Command

Let’s look at some practical examples to better understand how to use the Linux tail command:

See also  8 Types of Linux Shells: A Comprehensive Guide with Practical Examples

Example 1: Monitoring a Log File

tail -f /var/log/apache2/access.log

This command will continuously display new lines added to the Apache access log, which is invaluable for real-time monitoring of web server activity.

Example 2: Displaying a Specific Number of Lines

tail -n 50 large_file.txt

This command displays the last 50 lines of the file, which can be useful when you need to see more context than the default 10 lines.

Example 3: Combining Tail with Other Commands

You can use the tail command in combination with other Linux commands using pipes:

ls -l | tail -n 5

This command lists the contents of the current directory and displays only the last 5 entries.

Example 4: Using Tail with Head

You can combine the tail and head commands to display a specific range of lines:

tail -n 20 file.txt | head -n 10

This command displays lines 11-20 of the file (the last 20 lines, but only showing the first 10 of those).

Example 5: Following Multiple Files

tail -f file1.log file2.log

This command allows you to monitor changes in multiple files simultaneously.

Advanced Usage and Tips

  1. Handling Log Rotation: When monitoring log files that undergo rotation, use the -F option instead of -f. This option will continue to follow the file even if it gets renamed:
    tail -F /var/log/syslog
  2. Displaying Lines Starting from a Specific Line Number: You can use a plus sign with the -n option to display lines starting from a specific line number:
    tail -n +100 file.txt

    This displays all lines starting from line 100 to the end of the file

  3. Outputting to a File: You can redirect the output of the tail command to another file:
    tail -n 100 source_file.txt > last_100_lines.txt
  4. Using with grep: Combine tail with grep command to filter the output:
    tail -f /var/log/syslog | grep ERROR

    This command will display only the lines containing “ERROR” as they are added to the syslog.

Conclusion

The Linux tail command is an essential tool for viewing and monitoring the end of text files. Its versatility makes it invaluable for system administrators, developers, and anyone working with log files or text-based data streams. By mastering the tail command and its various options, you can significantly improve your efficiency in Linux system administration and log analysis tasks.

Remember, practice is key to becoming proficient with the tail command. Experiment with different options and combinations to find the most effective ways to use this powerful utility in your workflow.

See also  Display Contents of a Text File in Linux Terminal Using View Command
Leave a Reply

Your email address will not be published. Required fields are marked *

Free Worldwide Courses

Learn online for free

Enroll in Multiple Courses

Learn whatever your want from anywhere, anytime

International Language

Courses offered in multiple languages & Subtitles

Verified Certificate

Claim your verified certificate