Let’s learn about tail command in Linux with examples and their options, the tail command is mainly used to check the last lines of the files or log files.
Also, a continuous log file can be viewed by tail command. It is mainly used by System Admin to check the logs.
Q.How to view the last few lines using a tail command in Linux?
So, when we have to view the last few lines then use a tail command, which shows us the last 10 lines
So, the file tail_example contains 20 lines and if we have to see the last 10 lines.
Command: # tail tail_example
Also, we can use pipe(|) with the cat command like cat [file_name]|tail
Command: # cat tail_example|tail
Q.How to view the last few lines by N number of lines using a tail command?
Now we will use -n option to view the N number of lines in a file.
Command: # tail -n 5 tail_example
So, we have used -n 5 to get the output of the last five lines. If you have to see the last 12 lines use the option like -n 12 with a tail.
Q.How to view Multiple files using a tail command?
Command: # tail check_the_number_of_line tail_example
We are now getting the output of the two files with there filename in the place of header.
If you do not want that file name in the place of Header, use -q option to never print the file name.
Command: # tail -q check_the_number_of_line tail_example
Q.How to view filename using a tail command in Linux?
So, we have seen that by using the -q option, will not print the file name, but what if we have to print the file name for the single files?
Command: # tail -v tail_example
So, by using -v option we can print the file name in the place of Header place.
Q.How to view content by the size using a tail command in Linux?
So, to view the content by the size can use a -c option. By default, the size is in bytes
So, the tail_example has the size of 402 bytes, and have to see what content does the last 125 bytes contains.
Command: # tail -c 125 tail_example
What if we want to see the content after 125 bytes?
So for that, we have used +125 with -c option and by using this option we will not able to see the starting content of 125 bytes.
Command: # tail -c +125 tail_example
Q.How to check the log files using a tail command in Linux?
This is the most used term for the System Admin and Linux Admin in Industries.
If any things happen on the server-side, we have to check with the tail -f option command.
Command: # tail -f /var/log/nginx/access.log
So, -f option will append the data in output and we can see the file grows if any new logs generated.
Let’s have a look how we can see the two log files at the same time?
Command: # tail -f /var/log/nginx/access.log /var/log/nginx/error.log
So, we have the two files in the /var/log/nginx directories access.log and error.log
Cheatsheet of the tail command
Let’s Play QUIZ for the tail command
Want to read about head command follow the link. Thanks to READ the POST.