Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Tail Command in Linux?
3.
Syntax of Tail Command in Linux
4.
Example of Tail Command in Linux
5.
Linux Tail Command Options
5.1.
-c / --bytes
5.1.1.
Example
5.2.
-n /--lines
5.2.1.
Example
5.3.
-q / --quiet, --silent
5.3.1.
Example
5.4.
-v / --verbose
5.4.1.
Example
5.5.
Other Options
6.
Applications of Tail Command in Linux
7.
Frequently Asked Questions
7.1.
What is head command in Linux?
7.2.
What is tail command Linux?
7.3.
How to use tail -f in Linux?
7.4.
What is the tail option in Linux command?
7.5.
How do you tail the last 10 lines in Linux?
8.
Conclusion
Last Updated: Apr 25, 2024
Easy

Tail Command in Linux

Author Nidhi Kumari
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Early in the 1990s, Finnish software developer Linus Torvalds and the Free Software Foundation (FSF) developed the Linux operating system for computers. It has grown to be such a popular option for computers. Linux's many software and tools enable users to customise their systems and set them up to carry out almost any task.

Tail Command in Linux

A program or utility running from the command line is a Linux command. In this article, we will discuss the tail command in Linux. We will explain some examples of Tail commands. We will also cover the applications of Tail Command.

What is Tail Command in Linux?

The Linux tail command is a vital component of command-line tools. The purpose of the tail command in Linux is mostly to limit the output of a Linux command or to output the EOF(End of file: text). The last 10 lines will be selected by default. Still, we can adjust the number of lines by specifying it in the command.

The open-source operating system Linux has a set of fundamental commands called Coreutils(GNU Core Utilities), which includes the tail command.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of Tail Command in Linux

The syntax of the tail command in Linux is as follows:

tail [options] …[file_name]

 

One can also execute the tail command in Linux without any option. The syntax is as follows:

tail [file_name]

 

Options: These are a set of options that are available for tail command in Linux. We will discuss the options in detail.
 

File_name: Enter one or more files for the tail command to process under [file_name]. When a [file_name] is not specified, the tail reads the standard input.

Note: Keep in mind that the tail command is case-sensitive.

Example of Tail Command in Linux

Here is a basic example of the tail command in Linux.

Let’s consider two files, CNLibrary.txt and CNLinux.txt. The file CNLibrary stores various sections of the Code Studion Library, and the file CNLinux.txt contains names of various coding ninjas articles related to Linux.

Let’s see the complete content of the file using the cat command.

$ cat CNLibrary.txt

 

Output:

Preparation Guides
Web Technologies
Aptitude
DevOps
DSA
CP
Machine Learning
Deep Learning
Artificial Intelligence
NLP
DBMS
Big Data
Operating System
Python
C++
Java
C
Go
C#
Carbon
Ruby
CN
TOC

 

Now, we will use the tail command without any options.

$ tail CNLibrary.txt

 

Output:

Python
C++
Java
C
Go
C#
Carbon
Ruby
CN
TOC

 

Now, let’s discuss the options in detail.

Linux Tail Command Options

We will cover various options available for the tail command in Linux.

-c / --bytes

Short Option: -c

Long Option: --bytes

It restricts output to the final n bytes or the bytes that come after byte n.

Example

Here is an example of the -c / --bytes option.

$ tail -c -5 CNLibrary.txt

 

Similarly, one can use positive numbers also.

$ tail -c 5 CNLibrary.txt

 

-n /--lines

Short Option: -n

Long Option: --lines

This option limits output to the lines after line n or to the last n lines.

Example

Here is an example of the -n / --lines option.

$ tail -n 5 CNLibrary.txt

 

Output:

C#
Carbon
Ruby
CN
TOC

 

OR you can also write it as:

$ tail - 5 CNLibrary.txt

 

Output:

C#
Carbon
Ruby
CN
TOC

-q / --quiet, --silent

Short Option: -q

Long Option: --quiet, --silent

Using this option, your output shows the contents and hides the file names.

Example

Here is an example of the -q / --quiet, --silent option.

Without using the - q option:

$ tail CNLibrary.txt CNLinux.txt

 

Output:

CNLibrary.txt
Python
C++
Java
C
Go
C#
Carbon
Ruby
CN
TOC
     CNLinux.txt
Linux Special Variables
Linux - Commands and Arguments
Overview of Kali Linux
Linux - I/O Redirection
Linux - Using Arrays
Linux - Filters
Linux - Basic Operators
Linux - Shell Loop
Linux - Shell Loop Controls
Linux - Shell Substitution

 

-v / --verbose

Short Option: -v

Long Option: --verbose

It allows the tail command in Linux to print the file name before showing the contents every time.

Example

Here is an example of the -v / --verbose option.

$ tail -v CNLibrary.txt

 

Output:

==> CNLibrary.txt <==
Python
C++
Java
C
Go
C#
Carbon
Ruby
CN
TOC

Other Options

Here are a few more basic options that can be used with the tail command in Linux.

--help: This option is used to view the help file.
Syntax: 

$ tail --help

 

--version: This option is used to view the version.
Syntax: 

$ tail --version

Applications of Tail Command in Linux

Some of the important applications of the Tail Command in Linux are as follows:

  • One can use the Linux tail command to print the file's end.
     
  • To remove a directory's oldest backup files.
     
  • To check single or multiple server log files for modifications.
     
  • One can also track specific changes in the desired file.

Frequently Asked Questions

What is head command in Linux?

A command-line tool called head prints the first 10 lines of the given files. In case of multiple files data from each file is preceded by its file name. It is complementary to the tail command in Linux. One can also modify how many lines the head command can print using the -n command line option.

What is tail command Linux?

The tail command in Linux displays the last part of a file.

How to use tail -f in Linux?

To use tail -f, simply type tail -f filename. This command continuously displays new lines added to a file in real-time.

What is the tail option in Linux command?

The tail option in Linux command displays the last part of a file. It's commonly used to view the end of log files or other large text files.

How do you tail the last 10 lines in Linux?

To tail the last 10 lines of a file in Linux, use the command tail -n 10 filename. This will display only the last 10 lines of the specified file.

Conclusion

In this article, we have discussed the Tail command in Linux. The tail command in Linux is a versatile tool for viewing the end of files, particularly useful for monitoring log files or observing real-time changes. 

We hope this blog has helped you. We recommend you visit our articles:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Previous article
tr Command In Linux
Next article
Troubleshooting with Linux Logs
Live masterclass