How to use the tail command in Linux
This tutorials shows you how to use the tail command in Linux. ‘tail’ is a command that is used to display the last few of lines from a given input. By default, it will display the last 10 lines. The ‘tail’ command is useful for reading files where the relevant content is always appended to the end of the file such as logs.
Examples of using ‘tail’
The above example would list the last 10 (default) lines of the file /file/name.txt
tail /file/name.txt -n 100
The above example would list the last 100 lines in the file /file/name.txt. Change 100 to 50 to display the last 50 lines of the file.
tail -f /file/name.txt
This example displays the last 10 lines and then updates the file as new lines are being added. This is a great command to use to watch log files or logs in real-time.
tail -f /tmp/debug.log –retry
Sometimes, the file intended to tail may not be available when you run the tail command and it may get created later or the files becomes inaccessible . You can use the –retry option to keep on trying to open the file as in the above example. If the file is not available at the time, the following warning will be issued but the tail command will still be trying to open the file:
tail: warning: –retry is useful mainly when following by name
tail: cannot open `/tmp/log’ for reading: No such file or directory
tail -f access.log | grep 22.214.171.124
If you’re trying to view a file such as the Apache access log file that is updated frequently, you can pipe its output through the grep command to filter out only the content you want. In the above example, the access.log file is being watched for any IP addresses of 126.96.36.199
tail -c 5 /file/name.txt
Tail can also display any number of bytes from the end of a file rather than a desired number of lines. The bytes selected will include newline characters and therefore can span multiple lines. This is accomplished using the -c option followed by the number of bytes desired. In the above example the final five bytes of the file /file/name.txt will be printed.
To find information on the ‘grep’ command, please go to https://www.web24.com.au/tutorials/use-grep-command-linux