Linux tail Command: File End Display

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial explores the tail command in Linux, a utility designed to display the last lines of a text file. We will cover its basic usage and introduce various parameters for enhanced functionality.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") subgraph Lab Skills linux/tail -.-> lab-214303{{"`Linux tail Command: File End Display`"}} end

tail Command

The tail command is used to view the end of a file, making it particularly useful for monitoring log files, tracking real-time changes, or simply inspecting the concluding lines of a document.

Command Usage

Let's start with a basic example. We have a file called example.txt in the /home/labex/project directory, and we want to display the last 10 lines of a file.

terminal

Input:

tail /home/labex/project/example.txt

Output:

Imagine you have a list of your favorite fruits:

1. Apple
2. Banana
3. Orange
4. Grape
5. Strawberry

Now, let's use the head command to peek at the last few lines of this list.

This command displays the last 10 lines of the specified file (example.txt). Similar to the previous tutorials, this initial stage will focus solely on the basic usage of the tail command.

Parameters and Usage Examples

The tail command offers a variety of parameters to customize its behavior. We will introduce some of the most commonly used ones and provide usage examples in increasing order of complexity.

Option Parameter

The tail command supports various options to customize its behavior. Here are a few common options:

  • -n N: Display the last N lines of the file.
  • -c N: Display the last N bytes of the file.

Example Usage

1. Tail with Line Numbers (-n)

The -n option in the tail command is used to specify the number of lines to display from the end of a file.

Input:

tail -n 3 /home/labex/project/example.txt

Output:

5. Strawberry

Now, let's use the tail command to peek at the last few lines of this list.

In this example, tail -n 3 displays the last three lines of the file.

2. Tail with Character Count (-c)

The -c option in the tail command is used to specify the number of characters to display from the end of a file.

Input:

tail -c 71 /home/labex/project/example.txt

Output:

let's use the head command to peek at the last few lines of this list..

In this example, tail -c 50 displays the last 50 characters of the file.

Summary

In summary, the tail command is a versatile tool for inspecting the end of a text file. Whether you need to view a specific number of lines, bytes, or follow a file in real-time, tail provides an efficient solution for examining the concluding portion of a document.

Other Linux Tutorials you may like