Monday, January 31, 2011

Display Past Date and Time

Following are various ways to display a past date and time: 
 
$ date --date='3 seconds ago' 
Thu Jan  1 08:27:00 PST 2009 
 
$ date --date="1 day ago" 
Wed Dec 31 08:27:13 PST 2008 
 
$ date --date="1 days ago" 
Wed Dec 31 08:27:18 PST 2008 
 
$ date --date="1 month ago" 
Mon Dec  1 08:27:23 PST 2008 
 
$ date --date="1 year ago" 
Tue Jan  1 08:27:28 PST 2008 
 
$ date --date="yesterday" 
Wed Dec 31 08:27:34 PST 2008 
 
$ date --date="10 months 2 day ago" 
Thu Feb 28 08:27:41 PST 2008 

Display Current Date and Time in a Specific Format

Following are different ways of displaying the current date and time in 

various formats: 

$ date 

Thu Jan  1 08:19:23 PST 2009 

 

$ date --date="now" 

Thu Jan  1 08:20:05 PST 2009 

 

$ date --date="today" 

Thu Jan  1 08:20:12 PST 2009 

 

$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s 

18001 

 

$ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S' 

Current Date: 01/01/09 

Current Time:08:21:41 

 

$ date +"%d-%m-%Y" 

01-01-2009 

 

$ date +"%d/%m/%Y" 

01/01/2009 

 

$ date +"%A,%B %d %Y" 

Thursday,January 01 2009 

 

Following are the different format options you can pass to the date 

command: 

o    %D   date (mm/dd/yy) 

o    %d   day of month (01..31) 

o    %m   month (01..12) 

o    %y   last two digits of year (00..99) 

o    %a   locale’s abbreviated weekday name (Sun..Sat) 

o    %A   locale’s full weekday name, variable length 

(Sunday..Saturday) 

o    %b   locale’s abbreviated month name (Jan..Dec) 

o    %B   locale’s full month name, variable length 

(January..December) 

o    %H   hour (00..23) 

o    %I   hour (01..12) 

o    %Y   year (1970…) 

 

Friday, January 21, 2011

Set Hardware Date and Time

Before setting the hardware date and time, make sure the OS date and time 
is set appropriately as shown in the set-system-date-and-time
 
Set the hardware date and time based on the system date as shown below: 
 
# hwclock –systohc 
 
# hwclock --systohc –utc 
 
Use hwclock without any parameter, to view the current hardware date and 
time: 
 
# hwclock 
 
Check the clock file to verify whether the system is set for UTC: 
 
# cat /etc/sysconfig/clock 
 
ZONE="America/Los_Angeles" 
UTC=false 
ARC=false 

Set System Date and Time

To change the system date use:  
 
# date {mmddhhmiyyyy.ss} 
 
o    mm – Month 
o    dd – Date 
o    hh – 24 hour format 
o    mi – Minutes 
o    yyyy – Year 
o    ss – seconds 
 
For example, to set system date to Jan 31st 2008, 10:19 p.m, 53 seconds 
 
# date 013122192009.53 
 
You can also change system date using set argument as shown below. 
 
# date 013122192009.53 
 
# date +%Y%m%d -s "20090131" 
 
# date -s "01/31/2009 22:19:53"  
 
# date -s "31 JAN 2009 22:19:53" 
 
# date set="31 JAN 2009 22:19:53" 
 
To set the time only: 
 
# date +%T -s "22:19:53" 
 
# date +%T%p -s "10:19:53PM" 

Thursday, January 20, 2011

Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

Use shopt -s cdspell to correct the typos in the cd command automatically as 
shown below. If you are not good at typing and make lot of mistakes, this will 
be very helpful. 
 
# cd /etc/mall 
-bash: cd: /etc/mall: No such file or directory 
 
# shopt -s cdspell 
 
# cd /etc/mall 
 
# pwd 
/etc/mail 
 
[Note: By mistake, when I typed mall instead of mail, 
          cd corrected it automatically]