If you are using a Linux based Operating System, then you should know the basic Linux commands to work on it. Normally, Linux users facing troubles in handling the files and to troubleshoot the issues. When we work on a terminal or SSH, then we should be very careful about it. In this article, you will be explained completely about using the Tail Command in Linux.

If you are just looking for a command to display files in Linux, then you can use tail command. Tail command is used to list the lines of a file. If a file has lakhs and millions of lines, it is not easy to scroll down or check the last lines. In this case, we can use the Linux tail command.

Syntax: tail [option] [file]

Here I could explain to you better by adding a few examples. Here I am just using txt files for examples, the process will be the same for all type of files.

To start with, let me mention the file name & it’s content.

File 1: numbers.txt

Below, I have used “cat” to display all the lines in a file numbers.txt

$cat numbers.txt
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen

File 2: fonts.txt

Below, I have used “cat” to display all the lines in a file fonts.txt

$ cat fonts.txt
Abadi MT Condensed Light
Albertus Extra Bold
Albertus Medium
Antique Olive
Arial
Arial Black
Arial MT
Arial Narrow
Bazooka
Book Antiqua
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic

The Tail command comes with various options. Below are the various possible options/functions to use along with the tail command,

1. Without using any Option

If we use the tail command without mentioning any additional functions, then it will show us the last 10 lines of the file.

Example: Here we use the tail command simply followed by the file name. You could see that Linux command displays last 10 lines of the file.

$ tail fonts.txt
Arial Black
Arial MT
Arial Narrow
Bazooka
Book Antiqua
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic

2. -n number

To display the last “number” lines of a file, we can use this option. Here -n is to represent that we set a restriction on the number of lines to get displayed. Here we can use this command without using “n” but the symbol “” is mandatory to perform the exact action.

Example: Here we will display last 5 lines of a file fonts.txt

$ tail -n 5 fonts.txt
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic

(or)

$ tail -5 fonts.txt
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic

3.  +number

To display the content after a particular number of lines,  you can use this option. In this option, we should use a number with a positive symbol instead of a negative symbol. The output shows the data starting from the specified line number until the end.

For example: Here we will display the content from the 5th line of the file named fonts.txt

$ tail +5 fonts.txt
Arial
Arial Black
Arial MT
Arial Narrow
Bazooka
Book Antiqua
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic

4. -q

You can even list more than one file at a time using the tail command in Linux. So to list the lines from more than one file you should use “-q” before the file name.

For example: Here I would try to list both the files numbers.txt and fonts.txt using the Linux tail command but without using -q. Here we didn’t mention any line count or value, so it will list the last 10 lines of both files one after another with the file name in previous line.

$ tail fonts.txt numbers.txt
fonts.txt
Arial Black
Arial MT
Arial Narrow
Bazooka
Book Antiqua
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic
numbers.txt
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen

If we use the “-q” option along with multiple files name in a tail command, then the output will be similar to the below result,

$ tail -q fonts.txt numbers.txt
Arial Black
Arial MT
Arial Narrow
Bazooka
Book Antiqua
Bookman Old Style
Boulder
Calisto MT
Calligrapher
Century Gothic
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen

5. -f option

The option -f will be used by the system administrators to check the log file. Using this option, the system admins monitor the growth of the log file written by most of the programs as they are running. If new lines are written to the log, then the console will update with the new lines.

Example:

$tail -f logfile

 

In the above article, I have explained about using the tail command individually and also with various other options. So here Linux tail command will be used to display the lines of a file in Linux based operating system. But if you are deleting the files or directories using Linux command, then you should be very careful and you should do it safely. Because once we removed a file or folder in Linux systems then it will be a complicated process to recover back.

 

Follow me

Gowdham S

Technical Manager at SeekaHost Ltd
Love to learn new things & Love to create new things with Innovative Ideas. Having 5+ years of experience in Digital Marketing Strategies and expertise in Server Handling & Web Hosting Technologies.
Follow me
More in Linux
How to Safely Remove Files and Directories
How to Safely Delete Files and Directories Using Linux Command

Safely remove files and directories in Linux Operating System through SSH or Command Line Interface In this post, we will...

Close