Changing to the Previous Directory in the Linux Terminal

In this quick guide, we will show you how to navigate to the previous directory using the Linux terminal.

Linux Previous Directory

When you first start using the terminal on a Linux-based operating system, you might find it a little tricky to navigate your system.

Luckily, the Linux terminal has many tricks you can use that make navigating your folders a straightforward and surprisingly fast process.

This particular guide will show you two ways that you can navigate directories on Linux.

First, we will also show you how to use the terminal to change to the previous directory you were in. For example, if you swapped from “/etc/pimylifeup/” to “/var/www/” but want to quickly return to the previous directory without having to type in the full path again.

Secondly, we will also show you how to navigate to the parent directory from within the Linux terminal. For example, going from “/etc/pimylifeup/example” to “/etc/pimylifeup“.

Once you have learned both of these, you will find navigating a Linux system even simpler.

Swapping to the Previous Directory in the Linux Terminal

While using the terminal to navigate directories on Linux, the system keeps track of the previous directory in an environment variable. In particular, it stores the previous directory in a variable called “OLDPWD“.

Thanks to the operating system keeping track of the previous working directory, going back is a really easy process.

Within the Linux terminal, all you need to do to change to the previous directory is to use the cd command followed by a single hyphen (-).

cd -

The command above is the equivalent of the following, where we reference the “OLDPWD” variable. Of course, it’s much easier to remember a single hyphen than the “OLDPWD” variable.

cd $OLDPWD

Please note that the Linux terminal only keeps track of the previous working directory; it does not keep a history of the folders that you were in.

Example of Swapping to the Previous Directory

Let us run you through a quick example of how you can change to the previous directory so you have a better idea of how this works.

1. For this example, let us start by changing to a directory. We will want to return to this directory in a couple of steps.

cd /etc/netplan

2. Now, let’s say we had to change to another directory to perform some tasks quickly.

For this example, we will change into a directory where we set up a Terraria server.

cd /opt/servers/terraria

3. Now, once we are done and want to change to the directory we were previously in, we need to use the cd command followed by a hyphen within the Linux terminal.

Since we haven’t moved any directories, Linux should return us to “/etc/netplan/“.

cd -

When using the command this way, it will output the name of the directory you are changing into. To avoid this, you will have to type in the “$OLDPWD” variable rather than using the hyphen.

/etc/netplan

Moving to the Parent Directory in the Linux Terminal

Instead of changing to the previous directory, you might be interested in how to move up to the parent directory.

On Linux-based operating systems, a single dot (.) represents the current working directory. Two dots, however, represent the parent directory to the one you are in.

If you want to move up to the parent directory within the Linux terminal, you only need to use the cd command followed by a single space and then two dots (..), as we have shown below.

Linux will automatically interpret the two dots as you want to move to the directory above the one you are in.

cd ..

Example of Moving Back a Directory

1. For this example, start by moving into the directory “/home/pimylifeup/exampledirectory” by running the command below.

cd /home/pimylifeup/exampledirectory

2. Now, if we want to move up to the parent directory which is “/home/pimylifeup“, we can simply use the “cd ..” command as shown below.

cd ..

Typing in these two dots is significantly easier than typing out the path to the parent directory. For our example, we would have had to type out the following instead of simply typing two dots.

cd /home/pimylifeup/

Conclusion

Hopefully at this stage in the guide you will now have a better idea of how you can navigate your directories when using the Linux terminal.

This guide focused on how you can move up a directory as well as how to change to the previous directory you were in. Knowing both of these will make using the terminal a much faster and more pleasant experience.

Please drop a comment below if you have any issues with either of these methods that we covered within this tutorial.

If you want to learn more about using a Linux system, be sure to check out our many other Linux guides.

Leave a Reply

Your email address will not be published. Required fields are marked *