1. 程式人生 > >linux每日命令(4):pwd命令

linux每日命令(4):pwd命令

linux每日命令(4):pwd命令

閱讀目錄(Content)

Linux中用 pwd 命令來檢視”當前工作目錄“的完整路徑。 簡單得說,每當你在終端進行操作時,你都會有一個當前工作目錄。

在不太確定當前位置時,就會使用pwd來判定當前目錄在檔案系統內的確切位置。

回到頂部(go to top)

1.命令格式:

pwd [引數]

回到頂部(go to top)

2. 命令功能:

pwd 代表的是‘Print Working Directory’(列印當前目錄)。如它的名字那樣,‘pwd’會打印出當前工作目錄,或簡單的來說就是當前使用者所位於的目錄。它會打印出以根目錄 (/)為起點的完整目錄名(絕對目錄)

回到頂部(go to top)

3. 常用引數:

一般情況下不帶任何引數

引數 描述
-L 即邏輯路徑logical,當目錄為連線路徑時,顯示連線路徑
-P 即物理路徑physical,顯示實際物理路徑,而非使用連線(link)路徑

如果同時使用了‘-L‘和‘-P‘,‘-L‘會有更高的優先順序。如果沒有指定引數,pwd會避開所有的軟連結,也就是說會使用‘-P‘引數。

回到頂部(go to top)

4. 常用示例

1. 檢視pwd命令

命令:

man pwd

輸出:

PWD(1)                           User Commands                          PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks

       -P, --physical
              avoid all symlinks

       --help display this help and exit

       --version
              output version information and exit

       If no option is specified, -P is assumed.
 Manual page pwd(1) line 1 (press h for help or q to quit)

2. 顯示當前目錄所在路徑.

命令:

pwd

輸出:

[email protected]:~/PycharmProjects/py3_test$ pwd
/home/hc/PycharmProjects/py3_test

目錄結構如下:

[email protected]:~/PycharmProjects$ tree -L 2
.
├── FreshOnline
│   ├── apps
│   ├── db_tools
│   ├── extra_apps
│   ├── FreshMartOnline
│   ├── manage.py
│   ├── media
│   ├── README.md
│   └── requirements.txt
├── FreshOnline_env
│   ├── bin
│   ├── include
│   ├── lib
│   ├── lib64 -> lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
├── my_test
│   ├── 2018.log
│   ├── link2018 -> 2018.log
│   ├── ln2018
│   └── test
├── py3_test
│   ├── t1.py
│   └── venv
└── test
    └── my_test -> /home/hc/PycharmProjects/my_test

說明:目錄為連線路徑時,pwd -P 顯示出實際路徑,而非使用連線(link)路徑;pwd顯示的是連線路徑

示例如下:

PycharmProjects/test目錄下有一個my_test連線檔案,指向PycharmProjects/my_test目錄,
進入test目錄下的my_test目錄,使用pwd,顯示結果與pwd -L 一致,是邏輯(連線)路徑,要檢視實際物理路徑則使用pwd -P

[email protected]:~/PycharmProjects/test$ pwd
/home/hc/PycharmProjects/test
[email protected]:~/PycharmProjects/test$ ll
總用量 8
drwxr-xr-x 2 hc hc 4096 10月 23 13:38 ./
drwxrwxr-x 7 hc hc 4096 10月 23 13:30 ../
lrwxrwxrwx 1 hc hc   32 10月 23 13:38 my_test -> /home/hc/PycharmProjects/my_test/
[email protected]:~/PycharmProjects/test$ cd my_test
[email protected]:~/PycharmProjects/test/my_test$ pwd
/home/hc/PycharmProjects/test/my_test
[email protected]:~/PycharmProjects/test/my_test$ pwd -P
/home/hc/PycharmProjects/my_test
[email protected]:~/PycharmProjects/test/my_test$ pwd -L
/home/hc/PycharmProjects/test/my_test

3. 多層連線檔案時,顯示所有連線檔案最終指向的檔案全路徑

/root目錄下面有個dir1目錄,test連線檔案指向dir1目錄

/opt目錄下面有一個test連線檔案,指向/root/test連線檔案

通過cd命令進入/opt/test

pwd預設,只顯示連線檔案的全路徑

[email protected]:~# pwd
/root
[email protected]:~# ll     
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/
[email protected]:~# ll /opt/   
total 20
drwx------ 16 sgl  sgl  4096 Oct 17  2015 sgl
lrwxrwxrwx  1 root root   10 Apr 24 05:55 test -> /root/test
[email protected]:~# cd /opt/test/   
[email protected]:~# pwd      
/opt/test
[email protected]:~# pwd -P  
/root/dir1

pwd -P 顯示連線檔案最終指向的檔案的全路徑。注意這裡不是/root/test。