35

I can navigate down in directory using cd in the terminal. How do I navigate back up if I go too far?

user598527
  • 113
  • 8
ruckus
  • 453

4 Answers4

51

cd .. will bring you back exactly one directory up.

You can string together those to go up multiple directories, e.g. up 3

cd ../../..

Instead of typing cd .. multiple times, what you could to is to place the function bellow into your .bashrc somewhere at the top, save .bashrc, and run source .bashrc or just close and reopen a terminal. Now, you have a function that does cd.. exactly how many times you told it to.

function goUp {
  num=$1
  while [ $num -ne 0  ];do
    cd ..
    num=$((num-1))
  done
}

Demo:

$ cd /usr/share/backgrounds/                                                  

backgrounds:$ goUp 2

usr:$ 

Alternatively:

goup(){ 
    cd $(n=$1 awk 'BEGIN{
        for(i=1;i<=ENVIRON["n"];i++) 
            printf "../"}';) 
}

Note that such method brings you back along the symlinks. Here's what I mean:

$ namei "$PWD" 
f: /home/user/VirtualBox VMs/CentOS
 d /
 d home
 d user
 l VirtualBox VMs -> /mnt/ubuntu/vboxvms
   d /
   d mnt
   d ubuntu
   d vboxvms
 d CentOS

$ goup 2
$ pwd
/home/user

See also

4

I found a simple way to go up.

cd ../

./ means current directory

../means one level up directory

adazem009
  • 1,212
1

You can use popd and pushd too, to "checkpoint" or "bookmark", or as I tend to describe it; "set a spawn-point":

pushd ./ # set the spawn point to the current folder ./

go to another directory, like cd .. or whatever

popd     # get back to where we set pushd

This is, hopefully something useful for someone,

Pablo Bianchi
  • 17,371
Jane
  • 1,085
0
  1. For normal bash:
  • cd ..
  • cd -
  1. I suggest using oh-my-zsh instead of a typical shell. It has a number of aliases; concerning the one you asked, you type .. without cd. Very comfy.

Next, one may use several periods for more levels:

-='cd -'
...=../..
....=../../..
.....=../../../..
......=../../../../..

Moreover, for going upward any number of levels, just type the number

1='cd -1'
2='cd -2'
3='cd -3'
4='cd -4'
5='cd -5'
6='cd -6'
7='cd -7'
8='cd -8'
9='cd -9'

E.g.,

$ ~/Documents/Fld1/Fld2/Fld3
$ 3
$ ~/Documents