Hack 2. Use cd alias to navigate up the directory effectively

by Ramesh

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.

  1. # mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
  2.  
  3. # cd /tmp/very/long/directory/structure/that/is/too/deep
  4.  
  5. # pwd
  6. /tmp/very/long/directory/structure/that/is/too/deep
  7.  
  8. # cd ../../../../
  9.  
  10. # pwd
  11. /tmp/very/long/directory/structure

Instead of executing cd ../../../.. to navigate four levels up, use one of the following 4 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.

  1. alias ..="cd .."
  2. alias ..2="cd ../.."
  3. alias ..3="cd ../../.."
  4. alias ..4="cd ../../../.."
  5. alias ..5="cd ../../../../.."
  6.  
  7. # cd /tmp/very/long/directory/structure/that/is/too/deep
  8.  
  9. # ..4
  10. [Note: use ..4 to go up 4 directory level]
  11.  
  12. # pwd
  13. /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.

  1. alias ..="cd .."
  2. alias ...="cd ../.."
  3. alias ....="cd ../../.."
  4. alias .....="cd ../../../.."
  5. alias ......="cd ../../../../.."
  6.  
  7. # cd /tmp/very/long/directory/structure/that/is/too/deep
  8.  
  9. # .....
  10. [Note: use ..... (five dots) to go up 4 directory level]
  11.  
  12. # pwd
  13. /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.

  1. alias cd..="cd .."
  2. alias cd...="cd ../.."
  3. alias cd....="cd ../../.."
  4. alias cd.....="cd ../../../.."
  5. alias cd......="cd ../../../../.."
  6.  
  7. # cd /tmp/very/long/directory/structure/that/is/too/deep
  8.  
  9. # cd.....
  10. [Note: use cd..... to go up 4 directory level]
  11.  
  12. # pwd
  13. /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 4 directory level.

  1. alias cd1="cd .."
  2. alias cd2="cd ../.."
  3. alias cd3="cd ../../.."
  4. alias cd4="cd ../../../.."
  5. alias cd5="cd ../../../../.."