6

I use "cd -" to jump back and forth between two directories.

Is there a command that will rotate between three directories?

1
  • 2
    If the directories you are using are static, you can just add a permanent alias to your .bashrc file. If you add a full path you can use simple commands to jump between the directories.
    – Eugene S
    Feb 23, 2012 at 9:57

7 Answers 7

21

There are commands (builtins) to maintain a stack of directories: pushd and popd.

2
  • 3
    Care to elaborate a bit more? Given that the user didn't find these 'simple' commands, I reckon there's more to it then just mentioning their name
    – Ivo Flipse
    Feb 27, 2012 at 15:28
  • 2
    use man builtins to get info on pushd, popd, and dirs. Set up convenient aliases to navigate. e.g. after 2 pushes, pushd +1 will rotate through the 3 directories.
    – libjack
    Feb 29, 2012 at 20:07
9

pushd and popd can be used here, however unlike the former poster: see also this howto once you have populated the $DIRSTACK with directories you can also use dirs and cd ~1 to jump to directories in $DIRSTACK directly!

3

First add the second and third directories to the directory stack (your working directory is already in the stack).

pushd second/directory/path
pushd third/directory/path

Then rotate through your directories with the following command:

pushd +1
1

Maybe this is too obvious, open two or three terminals and run mc in them?

1

pushd/popd and $DIRSTACK solutions proposed in other answers are the "canonical" way.

Another option would be to use screen command. You open your directories in different sreens and switch between next/previous using shortcuts - Ctrl-a n for next and Ctrl-a p for previous. Or using Ctrl-a anyNumber to go to a specific screen. Or using Ctrl-a doublequote to display and visually select which screen you want.

Check all possible window navigation shortcuts.

0

Try using 'z' link. It remember which directories you have been to and you can jump around with short commands and abbreviated directory names.

0

like said before, but with just a little more words:

for bash, basically: instead of using cd use pushd to change directorys, so they are saved (meaning stacked)

pushd /home; pushd /var; pushd /log

To see the stack use dirs and for easier navigation (to get the numbers of the "stack-entries" use:

dirs -v

Output:

me@myhost:/home$ dirs -v
 0  /home
 1  /var
 2  /tmp

Now utilize these numbers with cd and ~ like:

cd ~1

But now these numbers are rearranged now and position "0" will change, so just pushd the directory to the top position twice (or use a dummy on position 0) like:

me@myhost:/home$ dirs -v
 0  /home
 1  /home
 2  /var
 3  /tmp

now 1..3 will keep there position I read this somewhere but do not know anymore, so sorry for not giving credit

(to release the current directory from stack/deleting it from the "dirs"-history use popd)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .