check logs in linux

Tail command to check logs in Linux

Many times you may need to see live logs, or monitor specific sections of log file. However, logs are ever growing files and it can be difficult to view it from the beginning. Tail is a useful Linux command to view the latest part of your file, including logs. You can also combine it with other Linux commands. Here is tail command to check logs in Linux.


Tail command to check logs in Linux

Here are different commands to check logs in Linux. Let us say your log file is located at /etc/nginx/nginx-access.log


1. Tail Command to View Logs in Linux

You can easily view the recent entries in your log file with the following command.

$ sudo tail /etc/nginx/nginx-access.log 

The above command will display last 10 lines in log file.


2. Tail Command to View Last 100 Lines

If you need to view specific number of lines from the end, then use the -n option followed by number of lines you want to see, as shown.

$ sudo tail -n 100 /etc/nginx/nginx-access.log


3. Tail Command to View Live Logs

Above commands only show output based on latest file that was used at the time of running the command. They don’t update the output automatically as time goes on. Sometimes you may want to view live log files in real-time. Tail command allows you to stream real-time log entries using -f or –follow option.

$ sudo tail -f /etc/nginx/nginx-access.log

Please note, in this case, the tail command will run in your foreground, regularly updating its output with latest log entries as they come.



4. Use Tail with other commands

You can also use the output of tail command with other commands, such as grep. Here is the example to search for log entries containing IP 65.54.43.32 in the last 1000 lines of your log file.

$ sudo tail -n 1000 /etc/nginx/nginx-access.log | grep 65.54.43.32

Tail is a useful command to view ever changing files such a log files. What we have shown in just a sample of possibilities. You can view all options available for tail command with ‘man tail’ command.

Also read:

How to Remove Unused Packages in Linux
How to Configure DNS Master-Slave Server in Linux
How to Restrict SFTP User to Specific Folder
How to Create Password Protected ZIP File in Linux
How to Determine File System Type in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *