The Wayback Machine - https://web.archive.org/web/20241029043653/https://www.geeksforgeeks.org/tail-command-linux-examples/
Open In App

Tail command in Linux with examples

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

It is the complementary of head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

Syntax of Tail Command in Linux

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

Let us consider two files having a name state.txt and capital.txt containing all the names of the Indian states and capitals respectively.

cat state.txt

Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal

Without any option it display only the last 10 lines of the file specified.

Example:

tail state.txt


Here we will only get names of last 10 states after using tail command.

tail command in Linux

tail command in Linux

Options and Pratical Examples of Tail Command in Linux

1. `-n` num Option in Tail Command in Linux

Prints the last ‘num’ lines instead of last 10 lines. num is mandatory to be specified in command otherwise it displays an error. This command can also be written as without symbolizing ‘n’ character but ‘-‘ sign is mandatory.

tail -n 3 state.txt


or

tail -3 state.txt


85

Tail command also comes with an ‘+’ option which is not present in the head command. With this option tail command prints the data starting from specified line number of the file instead of end. For command:

tail +n file_name, data will start printing from line number ‘n’ till the end of the file specified.

tail +25 state.txt

86

tail +n option in Linux

2. `-c` num Option in Tail Command in Linux

Prints the last ‘num’ bytes from the file specified. Newline count as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +num, it display all the data after skipping num bytes from starting of the specified file and by -num, it display the last num bytes from the file specified.

Note: Without positive or negative sign before num, command will display the last num bytes from the file specified.

With negative num

tail -c -7 state.txt

or

tail -c 7 state.txt

-c option in tail command in Linux

-c option in tail command in Linux (using negative)

With positive num

tail -c +263 state.txt

-c option in tail command in Linux (using positive)

-c option in tail command in Linux (using positive)

3. `-q` Option in Tail Command in Linux

It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

But before lets see text inside capital.txt file.

cat capital.txt

Amaravati
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar (summer), Jammu (winter)
Ranchi
Bengaluru
Thiruvananthapuram
Bhopal
Mumbai
Imphal
Shillong
Aizawl
Kohima
Bhubaneswar
Chandigarh
Jaipur
Gangtok
Chennai
Hyderabad
Agartala
Lucknow
Dehradun
Kolkata

Without using -q option

tail state.txt capital.txt

Without using -q option in tail command in Linux

Without using -q option in tail command in Linux

With using -q option

tail state.txt -q capital.txt

With using -q option in tail command in Linux

With using -q option in tail command in Linux

4. `-f` Option in Tail Command in Linux

This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added. As new lines are written to the log, the console will update with the new lines.

The prompt doesn’t return even after work is over so, we have to use the interrupt key to abort this command. In general, the applications writes error messages to log files. You can use the -f option to check for the error messages as and when they appear in the log file.

$ tail -f logfile

5. `-v` Option in Tail Command in Linux

By using this option, data from the specified file is always preceded by its file name.

tail -v state.txt

-v option in tail command in Linux

-v option in tail command in Linux

6. `–version` Option in Tail Command in Linux

This option is used to display the version of tail which is currently running on your system.

tail --version

To check version of tail command in Linux

To check version of tail command in Linux

Applications of tail Command in Linux

1. How to use tail with pipes(|):

The tail command can be piped with many other commands of the unix. In the following example output of the tail command is given as input to the sort command with -r option to sort the last 7 state names coming from file state.txt in the reverse order.

tail -n 7 state.txt

tail command

tail command

Using Tail command with pipe `|`

tail -n 7 state.txt | sort -r

Using Tail command with pipe `|`

Using Tail command with pipe `|`

It can also be piped with one or more filters for additional processing. Like in the following example, we are using cat, head and tail command and whose output is stored in the file name list.txt using directive(>).

cat state.txt | head -n 20 | tail -n 5  > list.txt
cat list.txt

using `>` operator in tail command

using `>` operator in tail command

What is happening in this command let’s try to explore it. First cat command gives all the data present in the file state.txt and after that pipe transfers all the output coming from cat command to the head command. Head command gives all the data from start(line number 1) to the line number 20 and pipe transfer all the output coming from head command to tail command. Now, tail command gives last 5 lines of the data and the output goes to the file name list.txt via directive operator. 2.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L

Conclusion

In this article we discussed the tail command in Linux serves as a complementary tool to the head command, specializing in displaying the last N lines of a given input. The default behavior prints the last 10 lines of specified files, with the option to include multiple file names, each preceded by its file name. The command’s syntax is succinct, using “tail [OPTION]… [FILE]…” for flexibility. Options such as -n, -c, -q, and -f provide users with precise control over the output, allowing customization based on line numbers, byte counts, file concatenation, and real-time log monitoring. The -v option ensures that data is consistently preceded by its file name. Additionally, the –version option provides details about the current version of the tail command. The command’s applications are diverse, ranging from straightforward data retrieval to complex piped operations with other Unix commands. In essence, the tail command proves to be an indispensable tool for efficiently navigating and monitoring files in the Linux environment.

Tail command in Linux with examples – FAQs

What is tail command with example?

The tail command in Linux is used to display the last part of a file. By default, it shows the last 10 lines of the specified file.

Example: To display the last 10 lines of file.txt:

tail file.txt

How to display last 10 lines of file in Linux?

To display the last 10 lines of a file in Linux, use the tail command:

tail file.txt

How to use Tails in Linux?

The tail command can be used with various options to customize its output. For example, to continuously monitor a file for new lines (like a log file), use the -f option:

tail -f file.txt

How do I read the last 20 lines of a file in Linux?

To read the last 20 lines of a file in Linux, use the tail command with the -n option:

tail -n 20 file.txt


Previous Article
Next Article

Similar Reads

Shell Script which Works Similar to the Unix Command HEAD TAIL
Prerequisites: Bash Scripting, Shell Function Library HEAD and TAIL command print a specified number of lines of a file from the beginning and from the end respectively. If we don't specify a number, the commands print the entire file. We will make a bash script display.sh which takes two to three arguments. -h or -t option to specify whether to pr
5 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
chpasswd command in Linux with examples
chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u
2 min read
lsmod command in Linux with Examples
lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command line to list all active kernel modules. lsmod Output:
1 min read
emacs command in Linux with examples
Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like vi, vim, nano, and the Emacs is that is faster, po
5 min read
host command in Linux with examples
host command in Linux system is used for DNS (Domain Name System) lookup operations. In simple words, this command is used to find the IP address of a particular domain name or if you want to find out the domain name of a particular IP address the host command becomes handy. You can also find more specific details of a domain by specifying the corr
2 min read
yes command in Linux with Examples
yes, the command in Linux is used to print a continuous output stream of a given STRING. If STRING is not mentioned then it prints 'y'; Syntax: of `yes` command in Linuxyes [STRING] Note: To stop printing please press Ctrl + C. Use of `yes` commandlet us say that we want to delete all the `.txt` files present in the current directory. Instead of wr
2 min read
bzdiff command in linux with examples
bzdiff command in Linux is used to compare the bzip2 compressed files. Bzcmp and bzdiff are used to invoke the cmp or the diff program on bzip2 compressed files. All options specified are passed directly to cmp or diff. If only one file is specified, then the files compared are file1 and an uncompressed file1.bz2. If two files are specified, then t
1 min read
bzcmp command in Linux with examples
The bzcmp utility is used to invoke the cmp utility on bzip2 compressed files. All options specified are passed directly to cmp. As internally bzcmp calls cmp, We can pass the cmp command options to bzcmp. If only one file is specified, then the files compared are file1 and an uncompressed file1.bz2. If two files are specified, then they are uncomp
2 min read
fold command in Linux with examples
The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command. Syntax of `fold` command in Linux fold [OPTION] [FILE] We can
3 min read
halt command in Linux with examples
This command in Linux is used to instruct the hardware to stop all the CPU functions. Basically, it reboots or stops the system. If the system is in runlevel 0 or 6 or using the command with --force option, it results in rebooting of the system otherwise it results in shutdown. Syntax: halt [OPTION]... .halt-command-table { border-collapse: collaps
2 min read
groupmod command in Linux with examples
groupmod command in Linux is used to modify or change the existing group on Linux system. It can be handled by superuser or root user. Basically, it modifies a group definition on the system by modifying the right entry in the database of the group. Syntax: groupmod [option] GROUP Files: The groupmod command has following files. /etc/group: Group A
2 min read
vmstat command in Linux with Examples
vmstat command in Linux/Unix is a performance monitoring command of the system as it gives the information about processes, memory, paging, block IO, disk, and CPU scheduling. All these functionalities makes the command vmstat also known as virtual memory statistic reporter. The very first report produced gives the average details since the last re
3 min read
diff3 command in Linux with examples
diff3 command is used to compare the three files line by line. It internally uses the diff command to compare. When three files are compared then the following output may come which have their own meaning: ==== : It means all the files are different. ====1 : File 1 is different. ====2 : File 2 is different. ====3 : File 3 is different. Syntax: diff
3 min read
bzless command in Linux with examples
bzless is a command similar to bzmore, but it has many more features. bzless does not have to read the entire input file before starting, so with a large file, it starts up faster than text editors like vi. bzless uses termcap (or terminfo on some systems), so it can run on a variety of terminals. Similar to bzmore it works on both types of files(c
2 min read
cron command in Linux with Examples
The cron is a software utility, offered by a Linux-like operating system that automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered without the intervention of a user. Dealing with a
4 min read
ex command in Linux with examples
ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transfer the text from one file to another. A user can q
4 min read
autoupdate command in Linux with examples
autoupdate command is used to update configure.in file in our Linux system to a newer Autoconf. Basically, it will update each TEMPLATE-FILE if given or 'configure.ac' file or 'configure.in' file to the syntax of the current version of Autoconf. The original files are backed up. Syntax: autoupdate [OPTION] ... [TEMPLATE-FILE...] Operation Modes: -h
1 min read
bzmore command in Linux with examples
bzmore command in Linux is used as a filter for CRT viewing of bzip2 compressed files, which are saved with .bz2 suffix. In simple words, using 'bzmore', the content of the file can be viewed on the screen without uncompressing the file. bzip2 is a block-sorting file compressor which compresses files using the Burrows-Wheeler block sorting text com
3 min read
colrm command in Linux with examples
colrm command in Linux is used for editing text in source code files, script files or regular text files. This command removes selected columns from a file. A column is defined as a single character in a line. It always starts at index 1 and not 0. If both start and end are specified, then the columns between them, including start and end will be r
1 min read
three90RightbarBannerImg