Linux: smarter file-system navigation on the command line

One of the most used commands on the terminal is the “cd” command because working from the command line most likely means that you need to go from your home directory to another directory, execute some commands and probably go back to your home directory, this is easy because “cd” without any directory returns you back to the home directory, but what happens if we have the following scenario:

  1. Goto /opt/app1/files/
  2. Goto ~/tmp
  3. Goto /opt/app1/files/

Well most likely you will do the following:

$ cd /opt/app1/files
$ cd ~/tmp
$ cd /opt/app1/files

But there is a cooler way, you can use a stack with the pushd and popd commands:

~$ cd /opt/app1/files
/opt/app1/files$ pushd ~/tmp
~/tmp$ popd

How it works?

pushd is like the cd command but with the additional step that before jumps to the new directory it stores the current to a stack.

popd is like the cd command but as a directory it takes the last inserted directory in the stack, note that the stack can be of any size, so you can backtrack all your current session pushd directories by entering popd many times.

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store