Pelicanux

Just A Few Random Words

Bash Date Command

Some funny things about date bash command, expecially while travelling back in time:

Hence, here is the way to get the date of the day three months ago, and three months and three days ago:

1
2
date -d"3 month ago" "+%G%m%d"
date -d"3 month ago 3 day ago" "+%Y%m%d"

The conversion 00 –> 01 and 30+1 = 31 or 01 accordingly are automatic!!

And last but not least, here is how one can grep events realized at a special day on log files:

1
grep `date +%Y-%m-%d -d "5 days ago"` test.txt

Here is how to translate timestamp from date and vice-versa

1
2
3
4
# Convert timestamp to date
date --date='@2147483647'
# Convert date to timestamp
date -d "2011/11/25 10:00:00" "+%s"

And that’s all! Ok, I was not in such a good shape today. Being regular at writing posts already pisses me off.