Chapter 1: Powerful CD Command Hacks
__Linux 101 Hacks
Hack 1. Use CDPATH to define the base directory forcd command
[ramesh@dev-db ~]# cd mail
~bash: cd: mail: No such file or directory
[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
/etc/mail
[ramesh@dev-db ~]# 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 inthe CDPATH variable, separating them with : , as shown below.
export CDPATH=.:~:/etc:/var
Hack 2. Use cd alias to navigate up the directory effectively
Method 1: Navigate up the directory using “..n”
In the example below, ..4 is used to go up 4 directory level.
Add the following alias to your~/.bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd http://www.cnblogs.com/.."
alias ..4="cd http://www.cnblogs.com/../.."
alias ..5="cd http://www.cnblogs.com/http://www.cnblogs.com/.."
# cd
/tmp/very/long/directory/structure/that/is/too/long
# ..4
# 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.
Add thefollowing alias to your ~/.bash_profile and re-login for the ..... (five dots) towork properly.
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd http://www.cnblogs.com/.."
alias .....="cd http://www.cnblogs.com/../.."
alias ......="cd http://www.cnblogs.com/http://www.cnblogs.com/.."
# cd /tmp/very/long/directory/structure/that/is/too/deep
# .....
# pwd
/tmp/very/long/directory/structure/
Method 3: Navigate up the directory using cd followed byconsecutive dots
In the example below, cd..... (cd followed by five dots) is used to go up 4directory level.
Add the following alias toyour ~/.bash_profile and re-login for the above cd..... (five dots) to workproperly.
alias cd..="cd .."
alias cd...="cd ../.."
alias cd....="cd http://www.cnblogs.com/.."
alias cd.....="cd http://www.cnblogs.com/../.."
alias cd......="cd http://www.cnblogs.com/http://www.cnblogs.com/.."
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd.....
#pwd
/tmp/very/long/directory/structure
Method 4: Navigate up the directory using cd followed by number
In the example below, cd4 (cd followed by number 4) is used to go up 4directory level.
alias cd1="cd .."
alias cd2="cd ../.."
alias cd3="cd http://www.cnblogs.com/.."
alias cd4="cd http://www.cnblogs.com/../.."
alias cd5="cd http://www.cnblogs.com/http://www.cnblogs.com/.."
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd4
# pwd
/tmp/very/long/directoy/structure
Hack 3. Perform mkdir and cd using a singlecommand
Be nice to combine both mkdir and cd in a single command.
Add the following to the .bash_profile and re-login.
# mkdir -p /tmp/subdir1/subdir2/subdir3
# cd /tmp/subdir1/subdir2/subdir3
# pwd
/tmp/subdir1/subdir2/subdir3
$ vi .bash_profile
function mkdircd() { mkdir -p "$@" && eval cd "\"\$$#\""; }
# mkdircd /tmp/subdir1/subdir2/subdir3
# pwd
/tmp/subdir1/subdir2/ssubdir3ubdir3
Hack 4. Use “cd -” to toggle between the last twodirectories
You can toggle between the last two current directories using cd - as shown below.
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/subdir1/subdir2/subdir3
# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
# cd -
# pwd
/tmp/subdir1/subdir2/subdir3
# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
Hack 5. Use dirs, pushd and popd to manipulatedirectory stack
You can use directory stack to push directories into it and later pop directoryfrom the stack. Following three commands are used in this example.
dirs: Display the directory stack
pushd: Push directory into the stack
popd: Pop directory from the stack and cd to it
dirs will always print the current directory followed by the content of thestack. Even when the directory stack is empty.
The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.
# popd
~bash: popd: directory stack empty
# dirs
~
#pwd
/home/ramesh
# ------------------
# mkdir /tmp/dir1
# mkdir /tmp/dir2
# mkdir /tmp/dir3
# mkdir /tmp/dir4
# cd /tmp/dir1
# pushd .
# cd /tmp/dir2
# pushd .
# cd /tmp/dir3
# pushd .
# cd /tmp/dir4
# pushd .
# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
# popd
# pwd
/tmp/dir4
# popd
# pwd
/tmp/dir3
# popd
# pwd
/tmp/dir2
#popd
# pwd
/tmp/dir1
# popd
~bash: popd: directory stack empty
Hack 6. Use “shopt -s cdspell” to automaticallycorrect mistyped directory names on cd
If you are not good at typing and make lot of mistakes, this willbe very helpful.
# cd /etc/mall
~bash: cd: /etc/mall: No such file or directory
# shopt -s cdspell
# pwd
/etc/mail