Do we have more history for cd?
You didn't specify which shell you are using, so let this be excuse to advertise zsh.
Yes, we do have more history for cd
, namely cd -2
, cd -4
etc. Very convenient is cd -
TAB, especially with completion system and colors enabled:
This is what I have in .zshrc:
setopt AUTO_PUSHD # pushes the old directory onto the stack
setopt PUSHD_MINUS # exchange the meanings of '+' and '-'
setopt CDABLE_VARS # expand the expression (allows 'cd -2/tmp')
autoload -U compinit && compinit # load + start completion
zstyle ':completion:*:directory-stack' list-colors '=(#b) #([0-9]#)*( *)==95=38;5;12'
And the result:
The command you are looking for is pushd
and popd
.
You could view a practical working example of pushd
and popd
from here.
mkdir /tmp/dir1
mkdir /tmp/dir2
mkdir /tmp/dir3
mkdir /tmp/dir4
cd /tmp/dir1
pushd .
cd /tmp/dir2
pushd .
cd /tmp/dir3
pushd .
cd /tmp/dir4
pushd .
dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
To answer your question regarding "more history". No the cd -
feature in Bash only supports a single directory that you can "flip" back to. As @Ramesh states in his answer. If you want a longer history of directories you can use pushd
and popd
to save a directory or return to a previous one.
You can also see the list of what's currently in the stack with the dirs
command.
A detailed explanation can be found from this answer titled: How do I use pushd and popd commands?.