在Linux中可以使用pwd命令來顯示當(dāng)前路徑。pwd是Print Working Directory的縮寫,其功能是顯示當(dāng)前所在工作目錄的全路徑,語(yǔ)法為“pwd [選項(xiàng)]”;主要用在當(dāng)不確定當(dāng)前所在位置時(shí),查看當(dāng)前目錄的絕對(duì)路徑。
本教程操作環(huán)境:centos8系統(tǒng)、Dell G3電腦。
由于 Linux 文件系統(tǒng)中有許多目錄,當(dāng)用戶執(zhí)行一條 Linux 命令又沒有指定該命令或參數(shù)所在的目錄時(shí),Linux 系統(tǒng)就會(huì)首先在當(dāng)前目錄(目前的工作目錄)搜尋這個(gè)命令或它的參數(shù)。因此,用戶在執(zhí)行命令之前,常常需要確定目前所在的工作目錄,即當(dāng)前目錄。
當(dāng)用戶登陸 Linux 系統(tǒng)之后,其當(dāng)前目錄就是它的主目錄。那么,如何確定當(dāng)前目錄呢?可以使用 Linux 系統(tǒng)的 pwd 命令來顯示當(dāng)前目錄的絕對(duì)路徑。
pwd命令概述
pwd
是Print Working Directory
的縮寫,其功能是顯示當(dāng)前所在工作目錄的全路徑。主要用在當(dāng)不確定當(dāng)前所在位置時(shí),通過pwd
來查看當(dāng)前目錄的絕對(duì)路徑。
pwd命令語(yǔ)法
pwd [選項(xiàng)]
參數(shù):
-
-L
:--logical
,顯示當(dāng)前的路徑,有連接文件時(shí),直接顯示連接文件的路徑,(不加參數(shù)時(shí)默認(rèn)此方式),參考示例1。 -
-p
:--physical
,顯示當(dāng)前的路徑,有連接文件時(shí),不使用連接路徑,直接顯示連接文件所指向的文件,參考示例2。 當(dāng)包含多層連接文件時(shí),顯示連接文件最終指向的文件,參考示例3。 -
--help
:顯示幫助信息。 -
--version
:顯示版本信息。
pwd命令示例
示例1:查看當(dāng)前所在路徑
[root@localhost var]# pwd /var
示例2:查看當(dāng)前所在路徑,不使用連接路徑
[root@localhost ~]# cd /var/ #進(jìn)入/var目錄,該目錄下有個(gè)mail連接文件,方便對(duì)比查看 [root@localhost var]# ll total 164 ... drwxr-xr-x 12 root root 4096 Apr 22 19:56 log lrwxrwxrwx 1 root root 10 Oct 17 2015 mail -> spool/mail drwxr-xr-x 2 root root 4096 May 11 2011 nis ... [root@localhost var]# cd mail/ #進(jìn)入mail目錄,mail為連接文件。 [root@localhost mail]# pwd #默認(rèn),使用連接文件,直接顯示連接文件全路徑。 /var/mail [root@localhost mail]# pwd -P #不使用邏輯路徑,連接文件最終指向的文件 /var/spool/mail
示例3:多層連接文件時(shí),顯示所有連接文件最終指向的文件全路徑
[root@localhost ~]# ll # /root目錄下面有個(gè)dir1目錄,test連接文件指向dir1目錄 total 12 drwxr-xr-x 2 root root 4096 Apr 24 05:51 dir1 lrwxrwxrwx 1 root root 5 Apr 24 05:54 test -> dir1/ [root@localhost ~]# ll /home/ #/home目錄下面有一個(gè)test連接文件,指向/root/test連接文件 total 20 drwx------ 16 sgl sgl 4096 Oct 17 2015 sgl lrwxrwxrwx 1 root root 10 Apr 24 05:55 test -> /root/test [root@localhost ~]# cd /home/test/ #通過cd命令進(jìn)入/home/test [root@localhost test]# pwd #默認(rèn),只顯示連接文件的全路徑 /home/test [root@localhost test]# pwd -P # 顯示連接文件最終指向的文件的全路徑。注意這里不是/root/test。 /root/dir1