Saturday, March 5, 2011

Using 'date' command smartly

Here I have shown some examples for using 'date' command in various forms. This will be helpful for deriving some logic in scripting.

Current time:
[root@hostxyz ~]# date
Fri Mar  4 12:37:29 CST 2011

Output only the day of week:
[root@hostxyz ~]# date +%a

Output the Time 1 hour before:
[root@hostxyz ~]# date +%Y%m%d%H%M%S -d  "1 hour ago"
[root@hostxyz ~]#

Output the Time 2 hours before:
[root@hostxyz ~]# date   --date='2 hour ago' +'%b %e %H:'
Mar  4 10:
[root@hostxyz ~]#

Finding Yesterday's day and assigning to a variable DAY_1:  
DAY_1=`(date --date='1 days ago' '+%a')`
echo $DAY_1

Finding day for two days ago and assigning to a variable DAY_2:
DAY_2=`(date --date='2 days ago' '+%a')`
echo $DAY_2

No comments:

Post a Comment