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 Algorithms, Competitive Programming, System 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!!