How to Use Tail Command in Linux
Table of Contents
Use Tail Command in Linux
Tail command prints last N number of lines from the given file. Tail command is complimentary of
head command. This command mostly used to monitor log files which are changing continuously in real time. Tails command by default prints last 10 lines if number of lines is not specified explicitly. In this tutorial you are going to learn how to use tail command in Linux.
Before you start to install Yarn on Ubuntu 18.04. You must have the non-root user account on your system with sudo privileges.
Following is the basic syntax for
tail [OPTION]... [FILE]...
OPTION : You can check the list of options available for tail command visiting this link.
FILE : Here you can use one sr multiple files.
Using Tail Command
Following command will print last 10 lines of
newfile.txt file. When no option specifies for number of lines it will by default prints 10 lines.
How to Print Specific Number of Lines
Following is the basic syntax to print specific number of lines from the give file. In following command
-n option is used to specify number of lines.
tail -n newfile.txt
To print last 20 lines of
newfile.txt you should use following command.
tail -n 20 newfile.txt
How to Display Multiple Files
If you want to display last 10 lines of multiple files then use following command. This command will print last 10 lines of
tail newfile1.txt newfile2.txt
To print, last 20 lines of multiple files use the following command.
tail -n 10 newfile1.txt newfile2.txt
Both above commands will print the last lines with their filenames also. If you want filename should not be printed the use
-q option with
tail -q newfile1.txt newfile2.txt
How to Watch a File For Changes
To Watch File changes in realtime use
tail -f newfile.txt
CTRL+c to exit.
You can use
-f (–follow) for monitoring log files in Linux. To check
access.log file use following command:
tail -f /var/log/nginx/access.log
CTRL+c to exit.
If you want to monitor file when its recreated use
tail command with
tail -F /var/log/nginx/access.log
How to Display Specific Number of Bytes
If you want to display specific number of Bytes
-c option is used called as –bytes with
To display last 100 bytes from the file
newfile1.txt use following command:
tail -c 100 filename.txt
To display last 1 kilobytes (1024 bytes) from the file
newfile1.txt run below command:
tail -c 1K filename.txt
In above command
k used to multiply with 1024. You can use
b to multiply with 512,
KB to multiply with 1000,
MB to multiply with 1000000 while
m to multiply with 1048576.
How to Use Tail Command With Another Commands
You can pipe tail command with another commands. By using tail command with other commands you can do useful operations as given below.
Following command is used to print last 100 bytes from
newfile.txt to output.txt file:
cat newfile.txt | tail -c 100 > output.txt
Use following command to print last 10 lines of file
newfile.txt sorted in reverse order:
tail -n 10 newfile.txt | sort -r
You have successfully learned how to use tail command in Linux. If you have any queries regarding this then please don’t forget to comment below.
- How to install Nginx on Ubuntu 18.04
- How to Install PhpStorm on Debian 10
- How to Change Hostname on CentOS 8
- How to install Nginx on CentOS 7
- How to grep All Sub-Directories for Files
- How to Install Netbeans on Debian 9
- How to Install PHP on CentOS 7
- How to enable SSH on Debian 9
- How to Recursively Search all Files for a String in Linux
- 17 Best Practical Examples of ls Command in Linux
- How to Install Mozilla Firefox on Linux Mint 19
- How to Install R on Linux Mint 19