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

Linux中用 pwd 命令來查看」當前工做目錄「的完整路徑。 簡單得說,每當你在終端進行操做時,你都會有一個當前工做目錄。json

在不太肯定當前位置時,就會使用pwd來斷定當前目錄在文件系統內的確切位置。app

1.命令格式:

pwd [參數]

2. 命令功能:

pwd 表明的是‘Print Working Directory’(打印當前目錄)。如它的名字那樣,‘pwd’會打印出當前工做目錄,或簡單的來講就是當前用戶所位於的目錄。它會打印出以根目錄 (/)爲起點的完整目錄名(絕對目錄)ui

3. 經常使用參數:

通常狀況下不帶任何參數this

參數 描述
-L 即邏輯路徑logical,當目錄爲鏈接路徑時,顯示鏈接路徑
-P 即物理路徑physical,顯示實際物理路徑,而非使用鏈接(link)路徑

若是同時使用了‘-L‘和‘-P‘,‘-L‘會有更高的優先級。若是沒有指定參數,pwd會避開全部的軟連接,也就是說會使用‘-P‘參數。code

4. 經常使用示例

1. 查看pwd命令

命令:orm

man pwd

輸出:ip

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. 顯示當前目錄所在路徑.

命令:ci

pwd

輸出:rem

hc@hc-virtual-machine:~/PycharmProjects/py3_test$ pwd
/home/hc/PycharmProjects/py3_test

目錄結構以下:it

hc@hc-virtual-machine:~/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

hc@hc-virtual-machine:~/PycharmProjects/test$ pwd
/home/hc/PycharmProjects/test
hc@hc-virtual-machine:~/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/
hc@hc-virtual-machine:~/PycharmProjects/test$ cd my_test
hc@hc-virtual-machine:~/PycharmProjects/test/my_test$ pwd
/home/hc/PycharmProjects/test/my_test
hc@hc-virtual-machine:~/PycharmProjects/test/my_test$ pwd -P
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/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默認,只顯示鏈接文件的全路徑

root@hc-virtual-machine:~# pwd
/root
root@hc-virtual-machine:~# 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/
root@hc-virtual-machine:~# 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
root@hc-virtual-machine:~# cd /opt/test/   
root@hc-virtual-machine:~# pwd      
/opt/test
root@hc-virtual-machine:~# pwd -P  
/root/dir1

pwd -P 顯示鏈接文件最終指向的文件的全路徑。注意這裏不是/root/test。

相關文章
相關標籤/搜索