歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux pwd命令學習心得

Linux pwd命令學習心得

日期:2017/2/28 14:22:37   编辑:Linux教程

你有沒有遇到過需要知道當前所在目錄卻無從得知?有沒有想要復制出當前所在目錄層次卻不知如何下手?俗話說有困難找警察,想知道目錄層次自然要找pwd了。那麼問題來了:

什麼是pwd

  pwd的意思是Print Working Directory,也就是打印工作目錄,意如其名,就是說打印出用戶當前所在目錄,它會打印出從根目錄(/)開始到當前所在目錄的完整路徑。這條命令是一條shell的內置命令,並且在大多數shell中都可以使用,如bash、Bourne shell,ksh、zsh等等。

命令格式:  

# pwd [OPTION]

常用參數:

選項 描述 -L (即邏輯路徑logical ) 使用環境中的路徑,即使包含了符號鏈接 -P (即物理路徑physical) 避免所有的符號鏈接 –help 顯示幫助並退出 –version 輸出版本信息並退出

如果同時使用了‘-L‘和‘-P‘,‘-L‘會有更高的優先級。如果沒有指定參數,pwd會避免所有的符號鏈接,也就是說會使用‘-P‘參數。好了下面介紹具體栗子。我們的栗子都是使用“/bin/pwd”的。那麼它和“pwd”有什麼區別呢?

pwd與/bin/pwd的區別

  這有什麼區別呢?直接使用“pwd”意味著使用shell內置的pwd。你的shell可能有不同版本的pwd。具體請參考手冊。當你使用的是/bin/pwd時,我們調用的是二進制版本的命令。雖然二進制的版本有更多的選項,但是它們兩者都能打印當前的目錄。好了下面繼續我們的pwd的栗子實戰。

8個pwd命令栗子

  1.打印當前目錄:

1 linuxidc@linuxidc:~$ /bin/pwd
2 
3 /home/linuxidc

  2.為文件夾創建一個符號鏈接(比如說在home目錄下創建一個htm鏈接指向/var/www/html)。進入新創建的目錄並打印出含有以及不含符號鏈接的目錄:

1 linuxidc@linuxidc:~$ ln -s /var/www/html/ htm
2 linuxidc@linuxidc:~$ cd htm

  3.從當前環境中打印目錄即使它含有符號鏈接:

1 linuxidc@linuxidc:~$ /bin/pwd -L
2 
3 /home/linuxidc/htm

  4.通過解析所有的符號鏈接來打印當前實際的物理目錄:

1 linuxidc@linuxidc:~$ /bin/pwd -P
2 
3 /var/www/html

  5.打印pwd命令的版本:

1 linuxidc@linuxidc:~$ /bin/pwd --version
2 
3 pwd (GNU coreutils) 8.23
4 Copyright (C) 2014 Free Software Foundation, Inc.
5 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
6 This is free software: you are free to change and redistribute it.
7 There is NO WARRANTY, to the extent permitted by law.
8 
9 Written by Jim Meyering.

  6.打印所有含有可執行pwd的路徑:

1 linuxidc@linuxidc:~$ type -a pwd
2 
3 pwd is a shell builtin
4 pwd is /bin/pwd

  7.存儲“pwd”命令的值到變量中(比如說:a ),並從中打印變量的值(對於觀察shell腳本很重要)。

1 linuxidc@linuxidc:~$ a=$(pwd)
2 linuxidc@linuxidc:~$ echo "Current working directory is : $a"
3 
4 Current working directory is : /home/linuxidc

  8.一次性獲取當前工作目錄以及先前工作的目錄:

1 linuxidc@linuxidc:~$ echo “$PWD $OLDPWD”
2 
3 /home /home/linuxidc

Copyright © Linux教程網 All Rights Reserved