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 ..
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!
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
- Locate Notepad++ unsaved files backup location - NotepadPlusPlus
- Android Developers Bluetooth Tutorial - Android
- [Solution] AWS Java SDK S3 AmazonS3Exception InvalidAccessKeyId - AWS
- How to switch between sftp and Terminal shell - FTP
- Fix: error: could not find `Cargo.toml` in Users or any parent directory - Rust
- [git] fatal: your current branch 'main' does not have any commits yet - Git
- How to know Notepad++ is 32-bit or 64-bit Version - NotepadPlusPlus
- SharePoint - Use Today's Date Time in list view filter and calculated column - SharePoint