In this article we are going to shows how to use tail command in Linux. If you want to learn about tail command in Linux with Examples then this post is ideal for you.
In the Linux, tail is a command line utility that is used to print last n lines of one or more files or piped data to standard output. By default, tail command prints last 10 lines of give file if number of lines is not specified. It is basically used to monitor and analyze the files changes in real time.
How to Use tail command in Linux
In the Linux, the basic syntax of the tail command is show as below:
tail [OPTION]… [FILE]…
Here,
- OPTION – you can get list of all available options here.
- FILE – one or more input file names.
When you run the tail
command without any options then it will display last 10 lines of given file.
$tail filename.txt
Display a Specific Number of Lines
If you want to prints specified the number of lines to be shown then use the -n
(--lines
) option with the tail
command as shown below:
$tail -n <NUMBER> filename.txt
For an example, if you want to print the last 25 lines of a given file, run the command below:
$tail -n 25 filename.txt
OR
$tail -25 filename.txt
Display Specific Number of Bytes
If you want to prints specified the number of bytes then use the -c
(--bytes
) optionwith the tail
command as shown below:
$tail -c <NUMBER> filename.txt
For an example, if you want to print the last 250 bytes of a given file, run the command below:
$tail -c 250 filename.txt
You can also use the above command as show below:
$tail -c 1k filename.txt
The above command display the last one kilobytes (1024) data of the given file.
Display Multiple Files
If you use multiple file with tail command, then it will display the last ten lines of each file.
$tail filename1.txt filename2.txt
If you want to print the last 25 lines from each given file, run the command below:
$tail -n 25 filename1.txt filename2.txt
Use Tail Command With Another Commands
You can also use tail command with other commands by using pipes.
For an example, Use below command to display last 25 lines of the given file filename.txt and sorted it in reverse order:
$tail -n 10 filename.txt | sort -r
Watch a File for Changes
If you want to monitor a file for changes then use the -f
(--follow
) option with tail command:
$tail -f filename.txt
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.
What is the meaning of -f in tail command
The -f option is very useful for monitoring log files. For an example, if you want to display the last 10 lines of the /var/log/nginx/error.log file, and monitor the file for updates you can run the command below:
$tail -f /var/log/nginx/error.log