国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > dirs 以获得更好的体验。

# Bash 的 dirs 命令

dirs [-clpv] [+N] [-N]

!subtitle:功能

查看当前工作目录和目录栈。

!subtitle:类型

Bash 内置命令。

!subtitle:参数

  • -c - 清空目录栈

  • -l - 不以波浪号(~)代替用户主目录

  • -p - 每行打印一个条目

  • -v - 每行打印一个条目,并打印序号(从 0 开始)

  • +N - 旋转目录栈,使得从栈顶向下的第 N 个目录位于栈顶

  • -N - 旋转目录栈,使得从栈底向上的第 N 个目录位于栈顶

# 示例

$ 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 ~
$ dirs -l               # 不以波浪号(`~`)代替用户主目录
/usr/share /tmp /home/user/primers
$ dirs -p               # 每行打印一个条目
/usr/share 
/tmp 
~
$ dirs -v               # 每行打印一个条目,并打印序号(从 0 开始)
 0  /usr/share 
 1  /tmp 
 2  ~

# 相关命令

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

# 手册

NAME
    dirs - Display directory stack.

SYNOPSIS
    dirs [-clpv] [+N] [-N]

DESCRIPTION
    Display directory stack.
    
    Display the list of currently remembered directories.  Directories
    find their way onto the list with the `pushd' command; you can get
    back up through the list with the `popd' command.
    
    Options:
      -c	clear the directory stack by deleting all of the elements
      -l	do not print tilde-prefixed versions of directories relative
    		to your home directory
      -p	print the directory stack with one entry per line
      -v	print the directory stack with one entry per line prefixed
    		with its position in the stack
    
    Arguments:
      +N	Displays the Nth entry counting from the left of the list
    		shown by dirs when invoked without options, starting with
    		zero.
    
      -N	Displays the Nth entry counting from the right of the list
    		shown by dirs when invoked without options, starting with
    		zero.
    
    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

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-27 09:38:14 创建于: 2025-11-27 09:38:14