How to cd backwards

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: CD backwards refers to navigating to previously visited directories in terminal shells without remembering the exact path, typically using commands like 'cd -' to return to the last directory or tools like 'pushd' and 'popd' for directory stack navigation. This technique improves command-line efficiency by allowing quick movement between working directories without typing full paths.

Key Facts

What It Is

CD backwards refers to the ability to navigate back to a previously visited directory in Unix and Linux command-line shells without re-entering the full file path. The most common implementation is the 'cd -' command, which changes to the last directory you were in before the current one. This feature leverages the OLDPWD (old previous working directory) environment variable that the shell automatically maintains during your session. The functionality has been a standard part of Unix shells since the 1980s and remains essential for efficient terminal work.

The concept originated from early Unix development when system administrators needed to quickly switch between two directories during maintenance tasks and system configuration. Bell Labs engineers recognized that most directory switching involves alternating between two locations rather than exploring a linear path. The implementation became standardized across all POSIX-compliant shells including Bash, Zsh, and Ksh. The simplicity and usefulness of 'cd -' made it one of the most-used shell shortcuts among experienced command-line users.

Beyond simple backwards navigation, shells offer directory stack management through 'pushd' (push directory) and 'popd' (pop directory) commands that treat directories like a stack data structure. The 'dirs' command displays all directories currently in the stack with their position numbers. Some shells support the DIRSTACK array variable allowing direct access to any previously visited directory by index number. Advanced third-party tools like 'z', 'autojump', and 'jump' use machine learning algorithms to predict which backwards directory you likely want to visit based on frequency and recency patterns.

How It Works

When you change directories using 'cd', the shell automatically stores your current directory path in the OLDPWD variable and updates PWD (present working directory) to your new location. The 'cd -' command reads the OLDPWD variable and changes to that stored path, simultaneously updating PWD and OLDPWD so they swap positions. This creates a toggle effect where typing 'cd -' twice returns you to your original directory, making rapid context switching between two locations instantaneous. The mechanism requires no additional setup or configuration beyond using a standard shell.

For practical example: A developer working with frontend and backend code alternates between '/home/user/project/frontend' and '/home/user/project/backend' directories throughout the day. Instead of typing the full path each time, they execute 'cd -' (2 keystrokes vs. 30+ keystrokes for full path). After working in frontend for 20 minutes, they type 'cd -' to immediately switch to backend, then 'cd -' again to return to frontend. The 'pushd' and 'popd' commands extend this to manage deeper navigation stacks; pushing three directories with 'pushd' creates a navigation stack where 'popd' removes directories in reverse order.

To implement CD backwards functionality: use 'cd -' for toggling between two directories, use 'pushd directory_name' to navigate while saving the previous location to a stack, use 'popd' to return to the previous location, and use 'dirs -v' to view your complete directory stack with index numbers. Access specific stack positions with 'cd ~#' where # is the index number shown in 'dirs -v' output. For advanced usage, configure shell aliases like 'alias cd-=cd' OLDPWD to create shortcuts, or install tools like 'autojump' via package managers for automatic backwards navigation based on directory frequency. Most experienced users create shell functions combining these features into custom navigation commands.

Why It Matters

Efficient directory navigation directly impacts developer productivity, with studies showing command-line users spend approximately 15-20% of development time entering directory paths. Mastering 'cd -' and directory stack commands reduces this overhead significantly, freeing cognitive load for actual coding work. Unix system administrators managing multiple server directories report 30-40% faster task completion when using backwards navigation effectively. The time savings compound across thousands of daily terminal interactions, creating substantial productivity gains.

Terminal-based workflows for software development, DevOps, system administration, and data science all benefit from rapid backwards navigation. Cloud engineers managing Kubernetes directories, Docker configurations, and infrastructure-as-code repositories frequently alternate between project directories requiring efficient switching. Data scientists navigating between dataset directories, model directories, and analysis notebooks improve workflow efficiency substantially with backwards navigation skills. System administrators managing multiple server configurations, log directories, and configuration file locations reduce error rates through faster context switching.

Future terminal interfaces and shell designs continue emphasizing navigation efficiency as developers work with increasingly complex project structures and distributed systems. Modern terminal multiplexers like Tmux and screen integrate directory stack concepts with session management for enhanced workflow capabilities. Shell frameworks like Oh-My-Zsh and Starship provide enhanced directory navigation plugins built on these historical concepts but with modern features. As cloud infrastructure and containerization create deeper directory hierarchies, backwards navigation remains critical to terminal efficiency.

Common Misconceptions

Many users believe 'cd -' returns to the parent directory (equivalent to 'cd ..'), when actually it returns to the previously visited directory regardless of hierarchy relationship. A common scenario: users navigate from '/home/user/projects/web' to '/var/log/apache', expecting 'cd -' to return to the parent '/var/log', when it actually returns to '/home/user/projects/web'. Understanding that 'cd -' references directory history not directory hierarchy prevents this confusion and allows proper tool selection for different navigation needs. Testing 'cd -' clearly demonstrates it always references OLDPWD not parent directories.

Another misconception assumes that directory stacks created by 'pushd' persist across terminal sessions, when actually the stack clears when you close the terminal window. Users expecting to reopen a terminal and use 'popd' to restore previous sessions become frustrated when the stack is empty. Understanding that directory stacks are session-specific encourages users to arrange multiple terminal windows or use terminal multiplexers like Tmux to maintain persistent directory contexts. Advanced users create scripts or configuration files to initialize directory stacks matching their workflows when starting new sessions.

Some assume that 'cd -' and 'popd' function identically and are interchangeable, overlooking that they operate on different mechanisms. 'cd -' toggles between exactly two directories (current and previous), while 'popd' removes directories from a potentially much larger stack one at a time. Attempting to navigate through three directories using only 'cd -' requires careful alternation and tracking, whereas 'pushd' and 'popd' handle multiple directory sequences cleanly. Selecting the right tool depends on specific navigation patterns rather than assuming all backwards navigation commands work identically.

Related Questions

What's the difference between 'cd -' and 'cd ..' commands?

'cd -' returns to the last directory you were in regardless of directory hierarchy, while 'cd ..' moves up one directory level in the folder structure. If you're in '/home/user/deep/nested/directory' and navigate to '/var/log', then 'cd -' returns to '/home/user/deep/nested/directory' but 'cd ..' returns to '/var'. Use 'cd -' for recent location history and 'cd ..' when you need to move up the hierarchy.

How do I navigate through multiple previous directories?

Use 'pushd directory_name' to navigate to a new directory while saving your current location, then use 'popd' to return to previous locations in reverse order. The 'dirs -v' command shows your complete directory stack with index numbers, and you can jump directly to any stack position using 'cd ~+#' where # is the index number. For very frequent navigation between many directories, consider installing tools like 'autojump' or 'z' that track directory visit frequency.

Can I see a list of all directories I've visited?

The 'history' command shows your recent commands including 'cd' commands, and you can pipe it to grep to find all directory changes with 'history | grep cd'. However, this only shows explicitly typed 'cd' commands, not programmatically navigated directories. The 'dirs -v' command shows your current directory stack, and shell history files like ~/.bash_history contain all previous commands including 'cd' entries from past sessions.

Sources

  1. cd (command) - WikipediaCC-BY-SA-4.0
  2. POSIX cd Command SpecificationCC-BY-SA-3.0

Missing an answer?

Suggest a question and we'll generate an answer for it.