2674

13 分钟

#Bash 的 pushd 命令

pushd [-n] [+N | -N | DIR]

功能

将当前工作目录压入栈顶,然后将工作目录切换到 DIR

如果不带 DIR 参数,则将当前工作目录与原先栈顶的目录交互。

类型

Bash 内置命令。

参数

  • -n - 仅将当前工作目录压入栈顶,不切换工作目录
  • +N - 旋转目录栈,使得从栈顶向下第的 N 个目录位于栈顶
  • -N - 旋转目录栈,使得从栈底向上第的 N 个目录位于栈顶
  • DIR - 目标目录

#示例

$ pwd # 查看当前目录 /home/user/primers $ pushd /tmp # 将 /home/user/primers 入栈,切换到 /tmp $ pwd # 查看当前目录 /tmp $ pushd /usr/share # 将 /tmp 入栈,切换到 /usr/share $ pwd # 查看当前目录 /usr/share $ dirs # 查看当前工作目录和目录栈 /usr/share /tmp /home/user/primers

#相关命令

命令说明
cd切换工作目录
pushd将当前工作目录压入栈顶,并切换工作目录
popd将目录栈的栈顶移除,并切换工作目录
dirs查看当前工作目录和目录栈

#手册

NAME pushd - Add directories to stack. SYNOPSIS pushd [-n] [+N | -N | DIR] DESCRIPTION Add directories to stack. Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories. Options: -n Suppresses the normal change of directory when adding directories to the stack, so only the stack is manipulated. Arguments: +N Rotates the stack so that the Nth directory (counting from the left of the list shown by `dirs', starting with zero) is at the top. -N Rotates the stack so that the Nth directory (counting from the right of the list shown by `dirs', starting with zero) is at the top. dir Adds DIR to the directory stack at the top, making it the new current working directory. The `dirs' builtin displays the directory stack. Exit Status: Returns success unless an invalid argument is supplied or the directory change fails. SEE ALSO bash(1) IMPLEMENTATION GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

创建于 2025/11/5

更新于 2025/11/5