If you spend a lot of time in the command line, you will have to
cd to different directories frequently. Instead of the
cd you can use
pushd command to switch to a different directory.
pushd command pushes the new directory into a "directory stack". Later you can return to your original location using
popd command. Both
pushd and
popd print the latest contents of the directory stack. You can also use
dirs command see the directory stack. Items in the directory stack are indexed from 0. You can reference any directory in the stack by typing
~ followed by the index. A sample session using the directory stack (user input is in bold font):
~% pushd /tmp
/tmp ~
/tmp% pushd /etc
/etc /tmp ~
/etc% pushd /var
/var /etc /tmp ~
/var% dirs
/var /etc /tmp ~
/var% popd
/etc /tmp ~
/etc% pwd
/etc
/etc% dirs
/etc /tmp ~
/etc% cd ~1
pingala /tmp % dirs
/tmp /etc ~
As the directory stack becomes larger, finding the index of a directory in the stack becomes harder. I use
-v flag to print the numeric index along with each item:
/tmp% dirs -v
0 /tmp
1 /etc
2 ~
If you are like me, you wouldn't remember to use
pushd. For users like us, the
zsh has an
auto_pushd option. When it's set, all
cd commands are treated like
pushd commands. With that option enabled, I use the shell normally, but zsh remembers the directories I have recently been to. I can use
dirs -v any time to get a list of recent directories and use the
~ notation to return to one those directories.