11 Dec 2008

Directory History

You can use the pushd and popd commands for jumping backwards and forwards through a directory stack in order, but you can also use an incredibly handy shortcut for jumping to an arbitrary directory in the stack.

Once you have some entries in your directory stack, jump between them at ease with "cd ~x", where x is the position of the desired directory in the stack. "dirs -v" will even number the stack entries for you, if you can't be bothered counting through them. Notice the current directory changing in my shell prompts below.

[ian@lyekka]~% pushd /var/log
/var/log ~
[ian@lyekka]/var/log% pushd /tmp
/tmp /var/log ~
[ian@lyekka]/tmp% pushd /usr/local/bin
/usr/local/bin /tmp /var/log ~
[ian@lyekka]/usr/local/bin% dirs -v
0       /usr/local/bin
1       /tmp
2       /var/log
3       ~
[ian@lyekka]/usr/local/bin% cd ~2
[ian@lyekka]/var/log% cd ~1
[ian@lyekka]/tmp%
You can, of course, also just popd back to the previous directory on the stack. See your shell manual for more information on these commands.