Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

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] 

Wednesday, January 19, 2011

Perform mkdir and cd using a single command

Sometimes when you create a new directory, you may cd to the new directory 
immediately to perform some work as shown below. 
# mkdir -p /tmp/subdir1/subdir2/subdir3 
 
# cd /tmp/subdir1/subdir2/subdir3 
 
# pwd 
/tmp/subdir1/subdir2/subdir3 
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add 
the following to the .bash_profile and re-login. 
$ vi .bash_profile 
 
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; } 
Now, perform both mkdir and cd at the same time using a single command as 
shown below: 
# mkdircd /tmp/subdir1/subdir2/subdir3 
 
[Note: This creates the directory and cd to it automatically] 
 
# pwd 
/tmp/subdir1/subdir2/subdir3 
OK CU next time 

Use cd alias to navigate up the directory effectively

When you are navigating up a very long directory structure, you may be using 
cd ..\..\ with multiple ..\’s depending on how many directories you want to go 
up as shown below. 
# mkdir -p 
/tmp/very/long/directory/structure/that/is/too/deep 
 
# cd /tmp/very/long/directory/structure/that/is/too/deep 
 
# pwd 
/tmp/very/long/directory/structure/that/is/too/deep 
 
# cd ../../../../ 
 
# pwd 
/tmp/very/long/directory/structure 
Instead of executing cd ../../../.. to navigate four levels up, use one of the 
following three alias methods: 
 
Method 1: Navigate up the directory using “..n” 
In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 
directory level, ..2 to go up 2 directory level. Add the following alias to your 
~/.bash_profile and re-login. 
alias ..="cd .." 
alias ..2="cd ../.." 
alias ..3="cd ../../.." 
alias ..4="cd ../../../.." 
alias ..5="cd ../../../../.." 
 
# cd 
/tmp/very/long/directory/structure/that/is/too/deep 
 
# ..4 
[Note: use ..4 to go up 4 directory level] 
 
# pwd 
/tmp/very/long/directory/structure/
Method 2: Navigate up the directory using only dots 
In the example below, ….. (five dots) is used to go up 4 directory level.  
Typing 5 dots to go up 4 directory structure is really easy to remember, as 
when you type the first two dots, you are thinking “going up one directory”, 
after that every additional dot, is to go one level up. So, use …. (four dots) to 
go up 3 directory level and .. (two dots) to go up 1 directory level. Add the 
following alias to your ~/.bash_profile and re-login for the ….. (five dots) to 
work properly. 
alias ..="cd .." 
alias ...="cd ../.." 
alias ....="cd ../../.." 
alias .....="cd ../../../.." 
alias ......="cd ../../../../.." 
 
# cd /tmp/very/long/directory/structure/that/is/too/deep 
 
# ..... 
[Note: use ..... (five dots) to go up 4 directory level] 
 
# pwd 
/tmp/very/long/directory/structure/ 
Method 3: Navigate up the directory using cd followed by consecutive dots 
In the example below, cd….. (cd followed by five dots) is used to go up 4 
directory level. Making it 5 dots to go up 4 directory structure is really easy to 
remember, as when you type the first two dots, you are thinking “going up 
one directory”, after that every additional dot, is to go one level up. So, use 
cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd 
followed by three dots) to go up 2 directory level. Add the following alias to 
your ~/.bash_profile and re-login for the above cd….. (five dots) to work 
properly. 
alias cd..="cd .." 
alias cd...="cd ../.." 
alias cd....="cd ../../.." 
alias cd.....="cd ../../../.." 
alias cd......="cd ../../../../.." 
 
# cd /tmp/very/long/directory/structure/that/is/too/deep 
 
# cd..... 
[Note: use cd..... to go up 4 directory level] 
 
# pwd 
/tmp/very/long/directory/structure 
Method 5: Navigate up the directory using cd followed by number 
In the example below, cd4 (cd followed by number 4) is used to go up 4 
directory level.  
 
alias cd1="cd .." 
alias cd2="cd ../.." 
alias cd3="cd ../../.." 
alias cd4="cd ../../../.." 
alias cd5="cd ../../../../.." 
CU next tutorial 

Use CDPATH to define the base directory for cd command

If you are frequently performing cd to subdirectories of a specific parent 
directory, you can set the CDPATH to the parent directory and perform cd to 
the subdirectories without giving the parent directory path as explained 
below. 
 
[ramesh@dev-db ~]# pwd 
/home/ramesh 
 
[ramesh@dev-db ~]# cd mail 
-bash: cd: mail: No such file or directory 
 
[Note: This is looking for mail directory under current 
directory] 
 
[ramesh@dev-db ~]# export CDPATH=/etc 
[ramesh@dev-db ~]# cd mail 
/etc/mail 
 
[Note: This is looking for mail under /etc and not 
under current directory] 
 
[ramesh@dev-db /etc/mail]# pwd 
/etc/mail 
 
To make this change permanent, add export CDPATH=/etc to your 
~/.bash_profile 
Similar to the PATH variable, you can add more than one directory entry in 
the CDPATH variable, separating them with : , as shown below. 
 
export CDPATH=.:~:/etc:/var 
 
This hack can be very helpful under the following situations: 
o    Oracle DBAs frequently working under $ORACLE_HOME, can set 
the CDPATH variable to the oracle home 
o    Unix sysadmins frequently working under /etc, can set the 
CDPATH variable to /etc 
o    Developers frequently working under project directory 
/home/projects, can set the CDPATH variable to /home/projects 
o    End-users frequently accessing the subdirectories under their 
home directory, can set the CDPATH variable to ~ (home 
directory)