Don't use the commandlinefu solution from the other answer: it's unsafe¹ AND inefficient.² Instead, if you are using bash, just use the following functions. To make them persistent, put them into your .bashrc. Note that I use glob order because it's built-in and easy. Typically glob order is alphabetical in most locales though. You'll get an error message if there is no next or previous directory to go to. In particular, you'll see the error if you try to next or prev while in the root directory, /.
## bash and zsh only!
# functions to cd to the next or previous sibling directory, in glob order
prev () {
# default to current directory if no previous
local prevdir="./"
local cwd=${PWD##*/}
if [[ -z $cwd ]]; then
# $PWD must be /
echo 'No previous directory.' >&2
return 1
fi
for x in ../*/; do
if [[ ${x#../} == ${cwd}/ ]]; then
# found cwd
if [[ $prevdir == ./ ]]; then
echo 'No previous directory.' >&2
return 1
fi
cd "$prevdir"
return
fi
if [[ -d $x ]]; then
prevdir=$x
fi
done
# Should never get here.
echo 'Directory not changed.' >&2
return 1
}
next () {
local foundcwd=
local cwd=${PWD##*/}
if [[ -z $cwd ]]; then
# $PWD must be /
echo 'No next directory.' >&2
return 1
fi
for x in ../*/; do
if [[ -n $foundcwd ]]; then
if [[ -d $x ]]; then
cd "$x"
return
fi
elif [[ ${x#../} == ${cwd}/ ]]; then
foundcwd=1
fi
done
echo 'No next directory.' >&2
return 1
}
¹ It doesn't handle all possible directory names. Parsing ls output is never safe.
² cd probably doesn't need to be terribly efficient, but 6 processes is a bit excessive.