What is a fast command line way to switch between multiple directories for system administration?
bash
's builtin pushd
with the +
and -
options can rotate the directory stack. The syntax can be a little confusing, perhaps because that stack is a zero-based array. These simple wrapper functions cycle through the directory stack:
# cd to next directory in stack (left rotate)
ncd(){ pushd +1 > /dev/null ; }
# cd to previous directory in stack (right rotate)
pcd(){ pushd -0 > /dev/null ; }
Test: set up a stack of four directories.
dirs -c # clear directory stack
cd /home ; pushd /etc ; pushd /bin ; pushd /tmp
Now /tmp is the current directory, and the stack looks like:
/tmp /bin /etc /home
Change to next directory in stack, (and show it), four times:
ncd ; pwd ; ncd ; pwd ; ncd ; pwd ; ncd ; pwd
Output:
/bin
/etc
/home
/tmp
Change to previous directory in stack, (and show it), four times:
pcd ; pwd ; pcd ; pwd ; pcd ; pwd ; pcd ; pwd
Output:
/home
/etc
/bin
/tmp
A note on cd -
: Wildcard's answer helped illustrate how cd -
doesn't use the $DIRSTACK array, (it uses the $OLDPW variable), so that cd -
doesn't effect $DIRSTACK the way a stack-based swap should. To correct that, here's a simple $DIRSTACK-based swap function:
scd() { { pushd ${DIRSTACK[1]} ; popd -n +2 ; } > /dev/null ; }
Test:
dirs -c; cd /tmp; \
pushd /bin; \
pushd /etc; \
pushd /lib; \
pushd /home; \
scd; dirs; scd; dirs
Output:
/bin /tmp
/etc /bin /tmp
/lib /etc /bin /tmp
/home /lib /etc /bin /tmp
/lib /home /etc /bin /tmp
/home /lib /etc /bin /tmp
Use pushd
and then the special names for the directories in your directory stack: ~1
, ~2
, etc.
Example:
tmp $ dirs -v
0 /tmp
1 /tmp/scripts
2 /tmp/photos
3 /tmp/music
4 /tmp/pictures
tmp $ cd ~3
music $ dirs -v
0 /tmp/music
1 /tmp/scripts
2 /tmp/photos
3 /tmp/music
4 /tmp/pictures
music $ cd ~2
photos $ cd ~4
pictures $ cd ~3
music $ cd ~1
scripts $
The most effective way to use pushd
in this way is to load up your directory list, then add one more directory to be your current directory, and then you can jump between the static numbers without affecting the position of the directories in your stack.
It's also worth noting that cd -
will take you to the last directory you were in. So will cd ~-
.
The advantage of ~-
over just -
is that -
is specific to cd
, whereas ~-
is expanded by your shell the same way that ~1
, ~2
, etc. are. This comes in handy when copying a file between very long directory paths; e.g.:
cd /very/long/path/to/some/directory/
cd /another/long/path/to/where/the/source/file/is/
cp myfile ~-
The above is equivalent to:
cp /another/long/path/to/where/the/source/file/is/myfile /very/long/path/to/some/directory/
I suggest you install fasd. It gives you the ability to quickly jump to any directory you've already been in by typing only a small fraction of its name.
Example: if you've visited /home/someName/scripts/
, you could jump there just by typing z scr
for example. It's way more convenient that remembering the ordering in the history stack or anything similar.