Change to previous working directory with cd command in Linux

Published: December 20, 2022

Last updated: December 20, 2022

Change to previous working directory with cd command in Linux

In this article, we cover how to change current working directory to previous working directory with cd command. Usually, we switch to previous working directory manually.

For instance, if the present working directory is /home/techpiezo/Downloads/ and the previous working directory was - /home/techpiezo/

Then, we issue following command to move to previous working directory -

cd /home/techpiezo/

So, this would take us to the previous working directory - /home/techpiezo/

Though switching back to the previous directory through above command would seem pretty easy. But, what if the directory names are long and confusing?

There is nothing wrong with the approach though. But, there is much easier and faster way to get this done where we don't have to enter the directory name or scroll the history to get back to previous working directory.

Change to previous working directory with cd command in Linux

Just issue the following command in terminal to get back to previous directory -

cd -

Let's understand it with the help of an example. The current working directory is - /home/techpiezo/Downloads/ and we assume that our previous working directory in the terminal was - /dev/shm/

We can issue the following command to confirm our current working directory -

pwd

This would return with the following -

/home/techpiezo/Downloads

Now, we want to move from our present working directory to the previous directory i.e. /dev/shm. Then, issue the following in terminal -

cd -

And, check for current working directory again -

pwd

This time around it would return with -

/dev/shm

In conclusion, we have covered how to change to previous working directory with cd command in Linux.