简述

目录是 Linux 的基本组成部分,目录管理包括目录的复制、删除、修改等操操作。

在 Linux 层次结构中,想要知道当前所处的目录,可以用 pwd 命令,该命令输出当前工作目录的完整名称。环境变量 OLDPWD 表示前一次的工作目录,环境变量 PWD 表示当前的工作目录。

| 版权声明:一去、二三里,未经博主允许不得转载。

命令介绍

  • 命令名称
    pwd
  • 命令全称
    Print Working Directory
  • 基本语法
    pwd [选项]…
  • 功能描述
    打印当前工作目录的完整文件名

命令选项

pwd 命令比较简单,默认情况下,不需要带任何参数,执行该命令显示当前路径。如果当前路径有软连接,显示链接路径而非实际路径,使用选项 P 可以显示当前路径的实际路径。

选项

说明

-L(​​--logical​​ 逻辑路径)

使用环境变量中的 PWD,即使其中包含符号链接

-P(​​--physical​​ 物理路径)

避免所有符号链接

​--help​

显示此帮助信息并退出

​--version​

显示版本信息并退出

使用范例

1.查看默认工作目录的完整路径

[wang@localhost ~]$ pwd
/home/wang

2.查看指定文件夹

[wang@localhost ~]$ cd /usr/bin/
[wang@localhost bin]$ pwd
/usr/bin

3.如果目录是个符号链接,pwd 显示链接(link)路径;pwd -P 显示实际路径。

[wang@localhost ~]$ ls -l /etc/init.d
lrwxrwxrwx. 1 root root 11 830 12:09 /etc/init.d -> rc.d/init.d
[wang@localhost ~]$ cd /etc/init.d/
[wang@localhost init.d]$ pwd
/etc/init.d
[wang@localhost init.d]$ pwd -P
/etc/rc.d/init.d
[wang@localhost init.d]$ pwd -L
/etc/init.d

4.查看上一次的工作目录与当前的工作目录

[wang@localhost ~]$ pwd
/home/wang
[wang@localhost ~]$ echo $OLDPWD

[wang@localhost ~]$ echo $PWD
/home/wang
[wang@localhost ~]$ cd /etc/init.d
[wang@localhost init.d]$ pwd
/etc/init.d
[wang@localhost init.d]$ echo $OLDPWD
/home/wang
[wang@localhost init.d]$ echo $PWD

5.当前目录被删除后,pwd 命令仍可显示该目录

[wang@localhost ~]$ mkdir test
[wang@localhost ~]$ cd test/
[wang@localhost test]$ pwd
/home/wang/test
[wang@localhost test]$ rm -rf ../test/
[wang@localhost test]$ pwd
/home/wang/test
[wang@localhost test]$ /usr/bin/pwd
/usr/bin/pwd: 在匹配的inode ".."

6.查看所有含有可执行 pwd 的路径

[wang@localhost ~]$ type -a pwd
pwd 是 shell 内嵌
pwd 是 /usr/bin/pwd

7.查看帮助信息

[wang@localhost ~]$ /usr/bin/pwd --help
用法:/usr/bin/pwd [选项]...
输出当前工作目录的完整名称。

-L, --logical 使用环境变量中的PWD,即使其中包含符号链接
-P, --physical 避免所有符号链接
--help 显示此帮助信息并退出
--version 显示版本信息并退出

注意:您的shell 内含自己的pwd 程序版本,它会覆盖这里所提及的相应
版本。请查阅您的shell 文档获知它所支持的选项。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告pwd 的翻译错误
要获取完整文档,请运行:info coreutils 'pwd invocation'

8.查看 pwd 命令的版本

[wang@localhost ~]$ /usr/bin/pwd --version
pwd (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。