天天看點

【Linux】一步一步學Linux——pwd指令(19)

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 指令概述
    • 02. 指令格式
    • 03. 常用選項
    • 04. 參考示例
    • 05. 附錄

01. 指令概述

pwd指令是print working directory中每個單詞的首字母縮寫,其功能正如所示單詞一樣,為列印工作目錄,即顯示目前工作目錄的絕對路徑。

02. 指令格式

pwd [選項]

03. 常用選項

顯示出 完整的 目前 活動目錄 名稱.
   -L        
      	列印 $PWD 變量的值,如果它命名了目前的工作目錄
   -P        
   		列印目前的實體路徑,不帶有任何的符号連結
    	預設情況下,pwd 的行為和帶 -L 選項一緻
	--help 
		顯示 幫助 資訊, 然後 退出
	--version 
		顯示 版本 資訊, 然後 退出
           

04. 參考示例

4.1 檢視預設工作目錄的完整路徑

[[email protected] ~]$ pwd
/home/deng
           

4.2 檢視指定檔案夾的路徑

[[email protected] ~]$ cd /usr/src/
[[email protected] src]$ pwd
/usr/src
           

4.3 如果目錄是個符号連結,pwd 顯示連結(link)路徑;pwd -P 顯示實際路徑。

[[email protected] ~]$ ls -l /etc/init.d
lrwxrwxrwx. 1 root root 11 11月  8 2018 /etc/init.d -> rc.d/init.d
[[email protected] ~]$ cd /etc/init.d
[[email protected] init.d]$ pwd
/etc/init.d
[[email protected] init.d]$ pwd -P
/etc/rc.d/init.d
[[email protected] init.d]$ pwd -L
/etc/init.d
[[email protected] init.d]$ 
           

4.4 檢視上一次的工作目錄與目前的工作目錄

[[email protected] ~]$ pwd
/home/deng
[[email protected] ~]$ echo $OLDPWD
/etc/init.d
[[email protected] ~]$ echo $PWD
/home/deng
[[email protected] ~]$ cd /etc/init.d/
[[email protected] init.d]$ pwd
/etc/init.d
[[email protected] init.d]$ echo $OLDPWD
/home/deng
[[email protected] init.d]$ echo $PWD
/etc/init.d
[[email protected] init.d]$
           

4.5 目前目錄被删除後,pwd 指令仍可顯示該目錄

[[email protected] ~]$ mkdir dir
[[email protected] ~]$ cd dir
[[email protected] dir]$ pwd
/home/deng/dir
[[email protected] dir]$ rm -rf ../dir/
[[email protected] dir]$ pwd
/home/deng/dir
[[email protected] dir]$ /usr/bin/pwd
/usr/bin/pwd: 在比對的inode ".." 上找不到目錄入口
[[email protected] dir]$ 
           

4.6 檢視pwd指令的類型

[[email protected] dir]$ type -a pwd
pwd 是 shell 内嵌
pwd 是 /usr/bin/pwd
[[email protected] dir]$
           

4.7 檢視pwd指令的版本

[[email protected] dir]$ /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>。
本軟體是自由軟體:您可以自由修改和重新釋出它。
在法律範圍内沒有其他保證。

由Jim Meyering 編寫。
[[email protected] dir]$
           

4.8 檢視pwd指令的幫助資訊

[[email protected] dir]$ /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'
[[email protected] dir]$ 
           

或者

[[email protected] dir]$ help pwd
pwd: pwd [-LP]
    列印目前工作目錄的名字。
    
    選項:
      -L        列印 $PWD 變量的值,如果它命名了目前的
        工作目錄
      -P        列印目前的實體路徑,不帶有任何的符号連結
    
    預設情況下,`pwd' 的行為和帶 `-L' 選項一緻
    
    退出狀态:
    除非使用了無效選項或者目前目錄不可讀,否則
    傳回狀态為0。
[[email protected] dir]$
           

05. 附錄

參考:【Linux】一步一步學Linux系列教程彙總