cd
- 使用 cd-
- 在Shell中可以通过 cd - 回到之前的目录。(注意cd后面有个空格)
- 其中,cd -中,-相当于变量$OLDPWD。即 cd - 相当于 cd $OLDPWD。
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd deploy
[root@localhost deploy]# pwd
/root/deploy
[root@localhost deploy]# cd -
/root
dirs
-
dirs 显示目录栈的内容。而目录栈是一个保存目录的栈结构,该栈结构的顶端永远存放着当前目录。
-
dirs 的常用参数
选项 含义 -p 每行显示一条记录。 -v 每行显示一条记录,同时展示该记录在栈中的index。 -c 清空目录栈。
pushd
- pushd 目录a 切换当目录a并将目录a加到当前目录栈的第二个位子。(注意目录栈的栈顶永远存放的是当前目录)。
- pushd 不带任何参数 将目录栈最顶层的两个目录进行交换。实际上就是实现了 cd -的效果。
- pushd +n 切换到目录栈中的第n个目录(这里的n就是 dirs -v命令中展示的index),并将该目录以栈循环的方式推到栈顶。需要注意栈循环的方式带来的栈中内容的变化规律。
popd
- popd 不带参数 将目录栈中的栈顶元素出栈。这时,栈顶元素发生变化,自然当前目录也会发生相应的变换(接上下文的执行现场)。
- popd +n 将目录栈中的第n个元素删除(这里的n就是命令 dirs -v 显示的目录index)。
- pushd -n/popd -n 这两个命令也支持-n,+表示从0开始,-表示从最后倒数开始。