Bash – How to tail a log file by time

bashcommand linelinuxshell-scripttail

Say my log file(log.txt) is something like

2014-01-01 22:30:30 something happened....
2014-01-01 22:30:31 something happened....
2014-01-01 22:30:41 something happened....

I want to tail this file to show last hour's log, and keep tailing..

i.e.

tail <some magic to specify last 1 hour> -f log.txt

Then the output is

2014-01-01 21:30:41 something happened....
...
2014-01-01 22:30:30 something happened....
2014-01-01 22:30:31 something happened....
2014-01-01 22:30:41 something happened....

Is there a tool to do this?

Best Answer

You can just use combination of grep and tail in oneliner.

grep "2014-01-01 21:" log.txt; tail -f log.txt

It will print everything from that hour, and keep tailing.

or you can also use awk to print everything from start of an certain hour to end of file, and keep tailing after it, this will allow you to tail last few hours if needed.

awk '/2014-01-01 21:/' log.txt; tail -f log.txt