There are many ways to display data from files and one of them is by using the “tail command”. This article is all about seeing data with the tail command in different ways on Ubuntu 20.04. The tail command, which is pre-installed in all Linux distributions, prints the last number (N) of data from the provided input. The last ten lines of a file can be displayed by the tail command by default. This command is very useful if we want to see just the last part of a bigger file. It will save screen space and also your time. It can keep a record of real-time file modifications.
This article will be very helpful for Linux users, especially newcomers, as they will learn different ways to display data from files using tail commands.
How to view Data using Tail command
There are various ways to display data using the tail command as mentioned below; you can use any of the methods according to your requirements.
- How to view Data from Single File Using Tail command
- How to view Data from Multiple File Using Tail command
- How to view Data from File Using Tail command with Different Options
The general syntax for the tail command is mentioned below:
$ tail [options] filename
How to view Data from Single File Using Tail command
With the help of the tail command, you can view the last part of the file, below mentioned command will display the last 10 lines of the “linux.txt” file.
$ tail linux1.txt
How to view Data from Multiple File Using Tail command
With the help of the tail command you can view the last part of more than one file, below mentioned command will display the last 10 lines of “linux1.txt” and “linux2.txt” files simultaneously.
$ tail linux1.txt linux2.txt
How to view Data from File Using Tail command with Different Options
There are multiple ways to view data with the help tail command by using different options.
Below are the options we can use with the tail command, and some of the options are explained with the help of examples.
- -n(number of lines)
- -c(number of bytes)
How to use the tail command with –n option
The “-n” defines the number of lines to be printed on the screen starting from the end of the file. If the number is not supplied in the command, an error will be displayed. We can also write ‘-‘ with the number instead of the ‘n’ character with this command.
The below-mentioned command will display the last 5 lines of the “linux1.txt” file
$ tail -n 5 linux1.txt
How to use the tail command with –c option
The latest ‘”num” bytes from the provided file are printed. If the new line is counted as one character, it will be counted as a byte by the tail command if it is printed. It is required to type -c followed by any number, depending on the need. If there is a positive sign (“+”) before the number, it will be displayed by skipping the number of bytes from the beginning of the supplied file. If there is a negative sign (“-“) before the number, it displays the final number of bytes. If there is no “+” or “-” sign before the number, it will display the last number of bytes from the file supplied.
The below-mentioned command will display the last 10 lines of the “linux1.txt” file. file.e
$ tail -c 10 linux1.tx
The below-mentioned command will display all data except the first 10 bytes of the “linux1.txt” file.
$ tail -c +10 linux1.txt
How to use the tail command with the –q option
If more than one file is specified, -q is used. As a consequence of this operation, the data from each file is not followed by the name of the file.
The below-mentioned command will show the combined data of files “linux1.txt” and “linux2.txt” without specifying the file name, as it did in the output of “display data of multiple files” as shown above:
$ tail -q linux1.txt linux2.txt
How to use the tail command with the –f option
This option is mostly used by system administrators to keep track of the size of log files generated by various Unix programs as they run. This option displays the file’s last 10 lines and updates when new lines are added. When new lines are being written on the log, the console will refresh them. We must use the interrupt key or “Ctrl+Z” to abort this command because the prompt does not return even after work is completed. In general, error messages are written to log files by the apps. An error message is checked by it when it appears in the log file.
The below-mentioned command will print the last 10 lines of linux1.txt, but the file will not be closed; it will prompt to write data or press Ctrl+Z to exit.
$ tail -f linux1.txt
How to use the tail command with the -v option
When this option is used, data from the chosen file is always followed by the name of the file.
The below-mentioned command will print the last 10 lines of linux1.txt followed by the filename “linux1.txt”:
$ tail -v linux1.txt
There are many ways to display data from a file; in this article, we discussed how we can view data with the help of the tail command. By default, it displays the last 10 lines from the specified file, but by using different options we can change the display of data by using the tail command. All these options are discussed in detail with examples in this article. After going through this article, you can get a hands-on tail command to display data from the file.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.