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.
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.
Say your current location is /usr/bin
bash-3.2$ pwd
/usr/bin
Now say you move to /Applications/XAMPP/htdocs.
bash-3.2$ cd /Applications/XAMPP/htdocs
Now if you run a few commands,
bash-3.2$ who
c2ctechtv console Aug 12 08:44
c2ctechtv ttys000 Aug 14 10:09
c2ctechtv ttys001 Aug 14 10:10
bash-3.2$ whoiam
bash: whoiam: command not found
At this moment, if you want to go back to the previous directory location, just make use of cd "$OLDPWD"
bash-3.2$ cd "$OLDPWD"
bash-3.2$ pwd
/usr/bin
Facing issues? Have Questions? Post them here! I am happy to answer!
- How to use Autocomplete and Autosuggestion in Shell Commands
- Bash How to Save Output of a Command to a Variable
- How to know the current shell you are logged in?
- How to Echo Bash Command to a File
- Bash Command to Get Absolute Path for a File
- How to Split a String based on Delimiter in Bash Scripting
- Bash: Command Line Arguments to Bash Script Examples
- Bash Command to Download a File From URL
- How to check if a Command Exists using Bash Script
- Ways to Increment a counter variable in Bash Script
- Know Bash shell version command
- Bash command to Read, Output and Manipulate JSON File
- Bash Command to Base64 Decode a String
- Bash Command to Check Python Version
- Bash: Command to Find the Length of a String
- What is $$ in Bash Shell Script- Special Variable
- Bash - How to check if a Command Failed?
- List all Username and User ID using Bash Command
- Command to Sort File In Reverse Order [Unix/Linux/macOS]
- bash: netstat: command not found
- Bash Command To Go Back To Previous Directory
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory
- How to check your IP using bash for Windows?
- Bash Command To Check If File Exists
- Convert String from uppercase to lowercase in Bash
- How to Comment out Code in PowerShell Script - Powershell
- Java: How to Add two Maps with example - Java
- Microsoft Office Mac Ventura: System Settings must be changed before Microsoft AutoUpdate can run - Microsoft
- Big Sur unsupported Mac [macOS] - MacOS
- Adding Custom ASCII Text Banner in Spring Boot Application - Java
- [Java] NoClassDefFoundError Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory - Java
- Reopen previously closed tab in Google Chrome - Chrome
- 30: How to Check if a Directory Exists or Not in Python Program - Python