Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date to timestamp in bash shell?

Tags:

bash

shell

I need to write a script to check one of my log files to check its last updated time. If it's greater than 90 minutes, I need to get alerted. But now I am stuck in timestamp conversion.

I have the below line in my code, but it's not working. It's throwing an error like ./monitor_log.sh: line 13: Aug: command not found.

modifeidTimestamp=$(date --date="`$lastModified`" +"%s");

The command below is working fine, but I have to use a variable. Can anyone help?

 date --date='Aug 25 06:07:30' +"%s"
like image 347
Nevin Thomas Avatar asked Aug 31 '25 16:08

Nevin Thomas


1 Answers

You can do:

lastModified='Aug 25 06:07:30'
modifeidTimestamp=$(date --date="$lastModified" +"%s")

echo "$modifeidTimestamp"
1440497250

No need to use reverse tick around lastModified variable. Just wrap it in double quotes.

like image 200
anubhava Avatar answered Sep 02 '25 11:09

anubhava