You are here:   Home 6 Awesome Linux cd command Hacks - Productivity Tip#3 for Geeks

dnrestcom -The simplest way to learn!

Just the simplest way to learn all the things.

Oct 15
2008

6 Awesome Linux cd command Hacks - Productivity Tip#3 for Geeks

Posted by: dnrestcom

Tagged in: Shell , Linux , hacks , bash

This article is part of the ongoing Productivity Tips for Geeks series. cd is one of the most frequently used command during a Unix session. In this article, I've provided 6 cd command hacks, which will boost your productivity instantly and make it easier to navigate the directory structure from command line.

Hack #1: Use CDPATH to define the base directory for cd command

If you are frequently doing 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
[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

This hack can be very helpful under the following situations:

  • Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
  • Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc
  • Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
  • End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)

Hack #2: 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 alias methods:

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 the .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/

 


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 the .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/

 


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 the .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

 

Hack #3: 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.

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

 

Continue reading....


Trackback(0)
Comments (0)add
You must be logged in to post a comment. Please register if you do not have an account yet.

busy

 Subscribe!

Or enter your email address:

Info