I can navigate down in directory using cd in the terminal. How do I navigate back up if I go too far?
- 113
- 7
- 403
- 1
- 4
- 7
-
3Possible duplicate of [Going to the parent directory of a file with cd?](http://askubuntu.com/questions/316628/going-to-the-parent-directory-of-a-file-with-cd) – DJCrashdummy Nov 29 '15 at 01:05
-
http://unix.stackexchange.com/a/81232/18237 great for working in multiple directory branches, and marking a "starting" point to come back to – Mateo Nov 29 '15 at 04:58
-
I know none of you want to admit that you were this stupid, but this question just hit 1000 views. so *haha* i know other people like me are out there. :P – ruckus May 16 '16 at 19:40
4 Answers
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
- 103,293
- 19
- 273
- 492
-
-
-
@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 – Sergiy Kolodyazhnyy Nov 29 '15 at 02:14
I found a simple way to go up.
cd ../
./ means current directory
../means one level up directory
- 982
- 3
- 14
- 26
-
1Upvote 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. – questionto42 Feb 11 '21 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 '21 at 21:23
-
`..`doesn't "stand for `../`, it's just the name of the parent directory. It's actually an entry (not sure off the top of my head if real or virtual) in the current directory. Do `ls -la` to see the entries for both `.` and `..`. – Jürgen A. Erhard Jul 01 '23 at 13:44
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,
- 14,308
- 4
- 74
- 117
- 953
- 4
- 18
-
2This doesn't answer the question and should have been a comment in my opinion – Guilherme Taffarel Bergamin Jul 03 '22 at 16:43
-
1you 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! :)** – William Martens Jul 09 '22 at 09:05
-
- For normal bash:
- cd ..
- cd -
- 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
- 13
- 3