Tips & Tricks


Working on Linux command line means switching between lot of directories. You are in a directory ‘A’, then you move to directory ‘B’. Now you want to come back to directory ‘A’. Typing the complete directory path for ‘A’ can be cumbersome sometimes. For this you can use ‘cd -‘ short cut.

Here is an example :

$ pwd
/home/YourName

$ cd /usr/local/bin/

$ cd –
/home/YourName

So we see that it’s easy to switch between two directories using cd- .

But, ‘cd -‘ resolves only a partial problem. It can only switch you back to last directory only. What if you switch between multiple directories and then want to come back to the first or some other desired directory? I mean, suppose you are in a directory ‘A’, then you switch to directories ‘B’ -> ‘C’ -> ‘D’ -> ‘E’ and then you want to again go back to directory ‘A’.

Well, for this, you can use the combination of ‘pushd’ and ‘popd’.

Here is an example :

$ pwd
/home/YourName

$ pushd /home/YourName
~ ~

$ cd /usr
$ cd /tmp
$ cd /proc

$ popd
~

$ pwd
/home/YourName

As you can see, first you pass the desired directory (to which you want to come back eventually) as argument to ‘pushd’ and then through ‘popd’ you can actually trigger a directory switch to that directory from anywhere on the command prompt.

Go Back to Menu