Search Authority

Master Linux Terminal: How to Go Back a Directory Easily

Navigating the Linux filesystem often requires moving between directories, and knowing how to go back a directory is a fundamental skill. Whether you are troubleshooting a servi...

Mara Ellison
Master Linux Terminal: How to Go Back a Directory Easily

Navigating the Linux filesystem often requires moving between directories, and knowing how to go back a directory is a fundamental skill. Whether you are troubleshooting a service or managing project folders, understanding directory traversal keeps your workflow efficient and error free.

This guide covers practical methods using commands and shortcuts so you can quickly return to a previous location in the terminal.

Command Action Use case Notes
cd .. Move up one level General navigation Relative path from current directory
cd ~ Go to your home directory Quick return to a known base Works even if you are deep in subdirectories
cd - Switch to the previous directory Toggle between two recent locations Maintains a simple back and forth history
pushd /path && popd Push current location, change, then return Stack based navigation Keeps a directory stack for complex workflows
cd $OLDPWD Return to the directory stored in OLDPWD Scripting and automation Relies on shell environment variables

Understanding Relative Paths with cd ..

The simplest way to go back a directory in Linux is by using cd ... The double dot refers to the parent directory of your current location, so this command moves you one level up the tree.

For example, if you are inside /home/user/projects/app, running cd .. takes you to /home/user/projects. This relative path approach is portable across different user environments.

Returning Home with cd ~

When you need to quickly go back to a predictable starting point, the home directory is usually the best choice. Using cd ~ jumps directly to your user home, regardless of how deeply nested your current path is.

This method is reliable in scripts and interactive sessions, and it avoids the need to remember or type the full path to your home directory.

Toggle with cd -

If you frequently move between two working directories, the cd - command lets you toggle back to the previous location. Each time you run cd -, the shell switches between the current directory and the last one you were in.

The shell also displays the path it is switching to, which makes it easy to confirm where you are without checking the prompt or running pwd.

Stack Based Navigation with pushd and popd

For more complex workflows, pushd and popd provide a directory stack that lets you move around and return to multiple previous locations in order.

Using pushd /path adds the current directory to the stack and changes to the new path, while popd returns you to the most recently pushed directory, making it easy to cycle through several folders.

Key Takeaways for Directory Navigation

  • Use cd .. to go back a single directory level with a relative path.
  • Use cd ~ to jump quickly to your home directory from any location.
  • Use cd - to toggle between the current directory and the previous one.
  • Use pushd and popd to manage a stack of directories for advanced workflows.
  • Refer to environment variables like $OLDPWD in scripts when you need the last directory path.

FAQ

Reader questions

How do I go back exactly one folder in the terminal?

Use cd .. to move up one directory level from your current location.

What is the fastest way to return to my home folder from anywhere?

Run cd ~ to jump directly to your home directory regardless of your current path.

How can I switch back and forth between two recent directories?

Use cd - to toggle between the current directory and the previous one stored in OLDPWD .

Can I return to a directory I visited earlier that is not just the last one?

Use pushd to save locations on a stack and popd to return to them in the order you saved.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next