Some handy linux gnu date commands

Using date command is pretty handy way to covert unix timestamp to string format and vice versa. This can also be used to print unix timestamp. Here are some handy date commands (using gnu date).

Print unix timestamp

Print unix timestamp in seconds (Mac or Linux)
$ date +%s

Print date in YYYYMMDD format

$ date +"%Y%m%d%H%M%S"

Print yesterday’s date

date --date="yesterday" +%s

You can also use “-1 day”, “+2 day” etc.

Convert unit timestamp to string format

$ date -d @1438364922
Fri Jul 31 23:18:42 IST 2015

You can print custom format also. e.g.

$ date -d @1438364922 +"%Y%m%d%H%M%S"

Additional steps for Mac

In case you are using Macbook, it comes with non GNU date. To install gnu date, install coreutils.

$ brew install coreutils

This will install gdate in addition to others utils on Mac as part of coreutils. Either replace date with gdate or edit the PATH envrironment variable. To edit the PTAH put these in your ~/.bash_profile:

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments