Go up several directories in linux

If there is a command I use a lot I will just make an alias.

You could type

alias ..='cd ..'
alias ...='cd ../..'

Then you can just use .. to go one level up and ... to go two levels up.


you can use pushd . to remember one directory and popd to go back to it.


cd ../../../../../../../

Also another useful navigation tip is if for example lets say you keep switching from a directory (call it A) to another (call it B) that's 7 directories up, in your case.

So if you're in directory A:

A> cd ../../../../../../../
B> // Now you're in directory B and want to go back to A
B> cd -

That will move right back to directory A. - expands to the previous directory you were in.


Make alias (in you ~/.bashrc)

function cd_up() {
  cd $(printf "%0.0s../" $(seq 1 $1));
}
alias 'cd..'='cd_up'

and use:

$ cd.. 7

UPD: Or make more powerfull variant, cd to dir name in current path:

# cd up to n dirs
# using:  cd.. 10   cd.. dir
function cd_up() {
  case $1 in
    *[!0-9]*)                                          # if no a number
      cd $( pwd | sed -r "s|(.*/$1[^/]*/).*|\1|" )     # search dir_name in current path, if found - cd to it
      ;;                                               # if not found - not cd
    *)
      cd $(printf "%0.0s../" $(seq 1 $1));             # cd ../../../../  (N dirs)
    ;;
  esac
}
alias 'cd..'='cd_up'                                # can not name function 'cd..'

use:

$ cd /home/user/documents/projects/reports/2014-10-01
$ cd.. doc
$ pwd
> /home/user/documents

Tags:

Linux

Cd