27

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

3

3 Answers 3

44

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

3
  • Added some more info, check it. Nov 28, 2015 at 23:36
  • That could be just for i in $(seq "$1"); do cd ..; done.
    – deltab
    Nov 29, 2015 at 2:08
  • @deltab true, could be done as well . . . .But I prefer using while + counter to simulate a for loop. I've asked a relevant question before on U&L site Nov 29, 2015 at 2:14
3

I found a simple way to go up.

cd ../

./ means current directory

../means one level up directory

2
  • 1
    Upvote because I did not know that the cd .. stands for cd ../, I thought it would be just like cd.. in Windows and did not understand the space in between until now. Feb 11, 2021 at 14:48
  • 1
    @questionto42 Yeah, .. is same as ../, as well as . vs ./. You can even join multiple slashes like this: .///. It's still same as .. There are also other cd tricks, like cd -, which will change the directory to the previous one. But that's a bit off topic :)
    – adazem009
    Feb 11, 2021 at 21:23
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,

Have a great day reader!

Regards

Will.

3
  • 1
    This doesn't answer the question and should have been a comment in my opinion Jul 3 at 16:43
  • 1
    you are right; in some sense - not directly answering; but as multiple methods should be known - so to speak; not only rely on 1 command; and this is a nice convenient way (especially for new ones) to do it; good to learn as well (NOTE: only my experience) @GuilhermeTaffarelBergamin +1 for that comment btw; upvoted! :) Jul 9 at 9:05
  • Thanks, +1 for yours as well Jul 10 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.