linux 操作系统下的dirs 命令介绍和使用案例
dirs
命令介绍
dirs
是一个 shell 内置命令,用于显示当前记住的目录列表。它通常与 pushd
和 popd
命令一起使用,允许用户在多个目录之间快速切换
基本语法
bash
dirs [OPTION] [+N] [-N]
- OPTION:可选参数,用于控制输出格式。
- +N:显示从左边数的第 N 个条目。
- -N:显示从右边数的第 N 个条目。
常用选项
选项 | 描述 |
---|---|
-c | 清空记住的目录列表。 |
-l | 不显示家目录的路径。 |
-p | 每行显示一个条目。 |
-v | 每行显示一个条目,并在前面加上从 0 开始的索引。 |
命令介绍
root@meng:~# dirs
~
root@meng:~# which dirs
root@meng:~# dirs --help
dirs: dirs [-clpv] [+N] [-N]
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.
root@meng:~#
命令案例
root@meng:~# dirs
~
root@meng:~# dirs -p
~
root@meng:~# dirs -v
0 ~
root@meng:~# dirs -l
/root
root@meng:~# pushd /data1
-bash: pushd: /data1: No such file or directory
root@meng:~# pushd /data
/data ~
root@meng:/data# pushd /tmp
/tmp /data ~
root@meng:/tmp# dirs
/tmp /data ~
root@meng:/tmp# dirs -p
/tmp
/data
~
root@meng:/tmp# dirs -v
0 /tmp
1 /data
2 ~
root@meng:/tmp# dirs -l
/tmp /data /root
root@meng:/tmp#