0

I am learning some elements of the Unix environment myself. I have tried to search up this question but perhaps I am not wording it correctly. It is quite possibly a very trivial question, too, but I am just not sure how to do it.

On terminal on my Mac, I know that I can do cd / to access my Unix directory files on terminal. However, how can I exit out of / and return to the previous directory I was in? I have tried cd .. but that does not work (why not? I don't know). I also don't want to use cd - because it is not necessary that my prior directory is the desired one.

6
  • The command cd .. go to upper directory, not previous one. And as / is the root you can't go in upper one. Jun 27, 2022 at 18:17
  • plain cd should take you to your home directory (usually /home/<username>)
    – Esther
    Jun 27, 2022 at 18:26
  • @Esther, and what if the previous directory was not user home? Jun 27, 2022 at 18:31
  • 1
    Then you would use cd -, but OP doesn't want that for some reason. It is VERY unclear where OP actually wants to go. He wants the "previous directory", which is somehow not necessarily the "prior directory".
    – Esther
    Jun 27, 2022 at 18:35
  • Sorry - it's my poor phrasing! This is what I meant: If I am in "/my_name_folder", and if I do "cd /", then "cd /etc", what I mean by previous directory is "/my_name_folder", whereas prior directory is "/". I wasn't sure to refer to "/my_name_folder" as the upper directory, since I am not knowledgeable about the sort of heirarchy here. Would this be called the home directory? Again, sorry for the terrible wording, but that is what I meant.
    – algebroo
    Jun 27, 2022 at 18:48

3 Answers 3

2

When you are in desired directory exec

pushd /

do some operations and when you want to return exec

popd

https://en.wikipedia.org/wiki/Pushd_and_popd

2

cd .. takes you to the current directory's parent. / is the filesystem's root and is it's own parent.

If the "$OLDPWD" (which cd - uses) is not what you want, just specify where you want to go

cd /
# then
cd /some/interesting/dir

If you want some shortcuts, investigate your shell's CDPATH variable.

2

The Unix file system is organized as a tree. The root of the tree is called /

enter image description here

In the picture above, you see a part of the tree. If you are in /, you can cd to etc or usr. In usr, you can cd to local or bin.

you can always specify the full path from root. If you are in /etc, you can cd /usr/local.

There are a number of special directories. . is the current directory. So, if you are in /usr, the following will get you to /usr/bin: cd ./bin. .. is the directory above. So, again from /usr, cd .. will put you in /. Or, if you're in /usr/bin, cd ../../etc will put you in /etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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