Pages

Monday, March 5, 2012

Use shopt In Linux


shopt is used in Toggle the values of variables controlling optional shell behavior.We can use cdspell along with Shopt to automatically correct mistyped directory names on cd
like

[root@vx111a ~]# shopt -s cdspell
[root@vx111a ~]# cd /var/ttp
/var/tmp
[root@vx111a tmp]# cd /var/tmp
[root@vx111a tmp]# cd /var/fmp
/var/tmp
[root@vx111a tmp]# cd /var/fup
/var/ftp
Read More

Push and Pop Directories on to Stack


Linux allows us to push directories on to the stack and pop them when needed.

Pushd : used to push directories on to the stack
Popd   : used to pop directories from the stack
Dirs    : allows us to see what directories are available on the stack

One thing to remember is that what ever the directory that is currently pushed is the one that popped first. Consider if I push /etc/ and then /var, then if I use popd , /var will the one that gets popped. Consider the sample

[root@vx111a ~]# cd /etc/X11/
[root@vx111a X11]# pwd
/etc/X11
[root@vx111a X11]# pushd $PWD
/etc/X11 /etc/X11
[root@vx111a X11]# cd /var/ftp/pub
[root@vx111a pub]# pushd $PWD
/var/ftp/pub /var/ftp/pub /etc/X11
[root@vx111a pub]# cd /root/perl/
[root@vx111a perl]# pushd $PWD
~/perl ~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# dirs
~/perl ~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# popd $2
~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# popd $3
/var/ftp/pub /etc/X11
[root@vx111a pub]# popd $4
/etc/X11

We can also pop a particular location by accessing them using $<Num> like popd $4 which pops out the 4 element on the directory stack.
Read More

Using CDPATH


We are already aware of the PATH variables in linux.The locations specified in the PATH are searched for something it that is not currently found in the current directory. Linux also supports CDPATH which provides directories to look when cd command changes to other locations like

[root@vx111a ~]# export CDPATH=/etc
[root@vx111a ~]# cd mail
/etc/mail
[root@vx111a mail]# pwd
/etc/mail
[root@vx111a mail]# cd -
/root
[root@vx111a ~]# pwd
/root
[root@vx111a ~]# export CDPATH=.:/etc:/var
[root@vx111a ~]# cd ftp
/var/ftp

Check then whenever we enter cd mail, it automatically takes us to the /etc/mail, But one thing we need to keep in mind is if we have a same directory with the same name as mail, even then it automatically takes us to the /etc/mail. In that case we need to add cd ./mail to go to mail directory in our current location. We can add the CDPATH to the .bashrc file with multiple directory locations and move between directories faster.
Read More