Monday, September 23, 2013

Date command with examples

Date command is most essentially used in scripting for date wise backups and in other scenarios.                                                                
                                                                     
                                                                     
                                             
    date +"%Y/%m/%d %H:%M"

is correct and 

    date +%Y/%m/%d %H:%M  

is not.
Please note that if you date conversion template contains spaces you need to put it in single or double quotes

If  you are OK with two year year representation you can use %y instead of %Y. There is also a macro %D that provides date in  mm/dd/yy format, so we can rewrite the previous example as

     date +"%D %H:%M"

Here  are most important build-in macros:

        %% a literal %
        %a locale's abbreviated weekday name (Sun..Sat)
        %A locale's full weekday name, variable length (Sunday..Saturday) %b locale's abbreviated month name (Jan..Dec)
        %B locale's full month name, variable length (January..December)
        %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)
        %d day of month (01..31)
        %D date (mm/dd/yy)
        %e day of month, blank padded ( 1..31)
        %H hour (00..23)
        %j day of year (001..366)
        %m month (01..12)
        %M minute (00..59)
        %n a newline
        %p locale's AM or PM
        %r time, 12-hour (hh:mm:ss [AP]M)
        %S second (00..60)
        %t a horizontal tab
        %T time, 24-hour (hh:mm:ss)
        %U week number of year with Sunday as first day of week (00..53)
        %V week number of year with Monday as first day of week (01..52)
        %w day of week (0..6); 0 represents Sunday
        %W week number of year with Monday as first day of week (00..53)
        %x locale's date representation (mm/dd/yy)
        %X locale's time representation (%H:%M:%S)
        %y last two digits of year (00..99)
        %Y -- ccyy year

Date also can work not with the current time, but with time of modification of the referenced file

    date -r /etc/passwd +"%y%m%d"

It also can serve as data calculator.
Various options for past days (GNU date only):

date +"%Y%m%d" -d sunday # GNU date
20060709

date +"%Y%m%d" -d last-sunday # GNU date
20060702

date +"%Y%m%d" -d last-week # GNU date
date -v -1m +"%Y%m%d" # BSD date
20060627

date +"%Y%m%d" -d last-month # GNU date
date -v -1w +"%Y%m%d" # BSD date
20060604

date +"%Y%m%d" -d last-year # GNU date
date -v -1y +"%Y%m%d" # BSD date
20050704

date +"%Y%m%d" -d next-week # GNU date
date -v 1w +"%Y%m%d" # BSD date
20060711

date +"%Y%m%d" -d next-month # GNU date
date -v 1m +"%Y%m%d" # BSD date
20060804

date +"%Y%m%d" -d next-year # GNU date
date -v 1y +"%Y%m%d" # BSD date
20070704

To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879

To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:

date -d "UTC 1970-01-01 1240596879 secs"
Fri Apr 24 13:14:39 CDT 2009

Or:

date -ud @1000000000
Sun Sep  9 01:46:40 UTC 2001

    Examples

date "+%-m/%-d/%y" 
7/4/06

date "+%Y%m%d"
20060704

    $ date  +"%b %d, %Y"
    Mar 14, 2010

    date -d yesterday +"%Y/%m/%d"
    2010/03/13

Yesterday assigned to variable

DATE=$(date -d yesterday +"%Y%m%d")
echo $DATE
20060704

To assign the time to a variable

START=`date '+%r'`
echo $START
03:06:02 PM
sleep 5
echo $START
03:06:02 PM

To show the time in a different timezone, the TZ environment variable is read, Timezone types is found in /usr/share/zoneinfo

OLDTZ=$TZ
export TZ=GMT; echo "GMT:               `date +\"%F %R (%Z)\"`"
GMT:               2008-10-31 12:30 (GMT)
export TZ=Europe/Stockholm; echo "Stockholm:    `date +\"%F %R (%Z)\"`"
Stockholm:    2008-10-31 13:30 (CET)
export TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur: `date +\"%F %R (%Z)\"`"
Kuala Lumpur:        2008-10-31 20:30 (MYT)
export TZ=US/Central; echo "Dallas:             `date +\"%F %R (%Z)\"`"
Dallas:             2008-10-31 07:30 (CDT)
export TZ=$OLDTZ

Using date for offset calculation:

date +"%Y%m%d" -d sunday
20060709

date +"%Y%m%d" -d last-sunday
20060702

date +"%Y%m%d" -d last-week
20060627

date +"%Y%m%d" -d last-month
20060604

date +"%Y%m%d" -d last-year
20050704

date +"%Y%m%d" -d next-week
20060711

date +"%Y%m%d" -d next-month
20060804

date +"%Y%m%d" -d next-year
20070704

No comments: