Bash Command To Go Back To Previous Directory





If you are using the bash shell on your Linux or macOS system and want to get back to the previous directory form the current directory you are in, you can make use of the cd change directory command followed by double dots ..


Example:

bash-3.2$ pwd
/Users/c2ctech/Desktop

bash-3.2$ cd ..
bash-3.2$ pwd
/Users/c2ctech

bash-3.2$ cd ..
bash-3.2$ pwd
/Users

bash-3.2$ cd ..
bash-3.2$ pwd
/
bash-3.2$ 

As you can see in the above example, using pwd command, I have displayed the current location and then made use of the cd .. to move to the previous directory until I reached to the root directory.

bash command to move to previous directory example

Note that there is a space between cd and the .. , if you do not add a space you will get an error bash: cd..: command not found

$  cd..
bash: cd..: command not found

If your requirement is to go back to the previous directory and not a step back, then you can make use of the $OLDPWD environment variable.

Let's take a look at an example.







Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap