Skip to content
Advertisement

Read last line of file after the file was modified

I have a problem with reading the last line of file in Linux Ubuntu. I have a file named auth.log and I’m trying to read it last line after new line was added (after file was modified).

I know i need to use tail -1 /var/log/auth.log to get last line but I don’t know how to check the file every time it was modified.

After reading the last line i need to check if it contains “login” word and if yes I need to send this line by email. As far as I know grep needs to be use with mail command.

Any help would be awesome, thanks!

Advertisement

Answer

Maybe something like this would work for you, using tail -f to continuously monitor the file:

while read -r; do
  echo "$REPLY" | mail your@email.address
done < <(tail -f /var/log/auth.log | grep --line-buffered login)

But this would be running continuously in the background, rather than as a cron job or something.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement