Threat Level: green Handler on Duty: Didier Stevens

SANS ISC: InfoSec Handlers Diary Blog - Scripting with Unix Date InfoSec Handlers Diary Blog


Sign Up for Free!   Forgot Password?
Log In or Sign Up for Free!

Scripting with Unix Date

Published: 2010-11-12
Last Updated: 2010-11-12 00:38:19 UTC
by Guy Bruneau (Version: 1)
11 comment(s)

I have been "playing" with the date command for a while in various Unix shell scripts and found the following date options quite useful.

Setting Unix system date and time

  • November 13, 06:30 a.m., 2010 do the following: date 111306302010

Unix epoch time to regular time

  • date –d @1289524456 will provide a result of "Thu Nov 11 20:14:16 EST 2010"

Unix date to epoch time

  • date +%s -d “2010-11-03” will provide a result of 1288756800

Unix epoch time to print only the time Fri Sep 10 10:00:01 EDT 2010

  • date -d @1288310401 +%k:%M will provide a result of 20:00 hours

Print yesterday's date (today - 1) in the Year-Month-Day format

  • date --date "-1 days" +"%Y-%m-%d" will produce a result of 2010-11-10

Print last month's date (today - 1 month) in the Year-Month-Day format

  • date --date "-1 month" +"%Y-%m-%d" will produce a result of 2010-10-11

You can also check the Unix man pages to display other time/date combination. If you know other date "tricks" you would like to share, you can send them via our contact page and I will added them to this diary.

-----------

Guy Bruneau IPSS Inc. gbruneau at isc dot sans dot org

Keywords: Unix Date
11 comment(s)
Diary Archives