다른 답변 의 commandlinefu 솔루션을 사용하지 마십시오. 안전하지 않으며 ¹ 비효율적입니다 .² 대신을 bash사용하는 경우 다음 기능을 사용하십시오. 그들을 영구적으로 만들려면에 넣으십시오 .bashrc. 글로브 순서는 내장되어 있고 쉬우므로 사용합니다. 대부분의 로케일에서는 일반적으로 글로브 순서 가 알파벳순입니다. 다음 또는 이전 디렉토리가 없으면 오류 메시지가 표시됩니다. 특히 루트 디렉토리에서 next또는 시도하는 prev동안 오류가 발생합니다 /.
## 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
}
¹ 가능한 모든 디렉토리 이름을 처리하지는 않습니다. 파싱 ls출력은 결코 안전하지 않습니다 .
²는 cd아마도 매우 효율적일 필요는 없지만 6 개의 프로세스는 약간 과도합니다.
[[ -n $foundcwd ]]bash와 zsh에서 똑같이 잘 작동합니다. 매우 좋으며 이것을 작성해 주셔서 감사합니다.