Skip to main content

Linux Tips: Return to Previous Directory

Linux Performance
By Keith Edmunds6 June 2017No Comments

A really quick and simple tip: cd - takes you back to the last directory you were in.

Here’s an example:

$ pwd
/home/kae
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/kae
$ pwd
/home/kae
$]

 

Slightly more involved are the pushd and popd commands. These allow you to create a “stack” of directories with pushd, and to return to the previous level with popd:

$ pwd
/home/kae
$ pushd /usr/bin
/usr/bin ~
$ pwd
/usr/bin
$ pushd /etc
/etc /usr/bin ~
$ pwd
/etc
$ popd
/usr/bin ~
$ pwd
/usr/bin
$ popd
~
$ pwd
/home/kae
$ popd
bash: popd: directory stack empty
$

Although pushd and popd are more powerful than a simple cd -, in reality they’re more complex than most people need.

Could this Linux Tip be improved? Let us know in the comments below.