linux pwd命令详解


              peter@initroot:~$ pwd [-LP]
              显示当前工作目录的绝对路径名
              选项:
              -L, --logical
              不管路径名是否是符号链接,都会显示环境变量$PWD的值,这里其实可以理解为如果路径名是个符号链接,则显示符号链接,不显示实际的物理路径名
              -P, --physical
              如果路径名是个符号链接,则不显示符号链接,显示实际的物理路径名,即符号链接指向的路径名
              --help
              显示在线帮助并退出
              --version
              输出版本信息并退出
              pwd默认为带-L选项
              如果没有指定参数,pwd的默认参数是-L
              Exit Status:
              Returns 0 unless an invalid option is given or the current directory cannot be read.
            
pwd为Print Working Directory的缩写,pwd命令可以打印当前工作目录。 我们刚才提到,命令行提示符已经将当前工作目录显示出来了,随着工作目录的切换而发生变化。但是有的用户对shell的命令提示符做了一些个性化配置(这方面的内容可以参考: linux bash操作环境配置 ), 工作目录部分可能没有完全显示出来,这时候我们就可以用pwd命令显示当前的工作目录:
              peter@initroot:~$ pwd
              /home/peter
            
pwd命令只有两个选项参数,分别是-L和-P,这两个选项都是针对符号链接进行处理的,
如果当前工作目录是个符号链接,
-L显示符号链接路径名,-P显示符号链接指向的实际物理路径名。
我们平时敲的pwd命令,默认是带-L参数的,即如果目录参数是一个符号链接,只会显示符号链接路径名。如果相显示符号链接指向的真实路径,就加上-P选项。
我们通过mkdir命令新建一个目录vimplugin,然后通过ln命令建立一个到vimplugin的软链接tovimplugin:
              peter@initroot:~$ mkdir vimplugin
              peter@initroot:~$ ln -s vimplugin tovimplugin
              peter@initroot:~$ ls -ld vimplugin tovimplugin
              lrwxrwxrwx 1 peter peter    9 Jan  7 16:29 tovimplugin -> vimplugin
              drwxr-xr-x 2 peter peter 4096 Jan  7 16:28 vimplugin
            
我们通过ls -ld列出两个文件的详细信息,可以看到tovimplugin为链接到vimplugin的链接文件。 现在我们通过cd命令进入tovimplugin,然后分别通过pwd和pwd -P查看输出的区别:
              peter@initroot:~$ cd tovimplugin
              peter@initroot:~/tovimplugin$ pwd
              /home/peter/tovimplugin
              peter@initroot:~/tovimplugin$ pwd -P
              /home/peter/vimplugin
            
通过上面的输出我们可以很容易的看出pwd和pwd -P的区别了,如果目录是链接文件,pwd显示的是链接文件本身的路径, 而pwd -P显示的是链接文件所指向的实际目录路径。 现在我们分别通过rm和rmdir命令将链接文件tovimplugin和目录vimplugin删除:
              peter@initroot:~$ rm -rf tovimplugin
              peter@initroot:~$ rmdir vimplugin
            
通过pwd命令,我们一下子又认识了好几个命令rm、rmdir、ln。 所以在linux下的学习总是会出现这样的情况,各个知识点都是相互联系的,不可能孤立的去学习某个知识。其实这也有个好处,那就是以点带面,通过一个知识点,可以迅速的掌握好几个知识点。

pwd用来显示当前工作目录的绝对路径名,为print the name of the current working directory的缩写。
当你突然忘记在哪个目录下了,就可以用pwd打印一下当前所在的工作目录。
这个命令只有两个选项参数,分别是-L和-P,这两个选项都是针对符号链接进行处理的,
如果当前工作目录是个符号链接,
-L显示符号链接路径名,-P显示符号链接指向的实际物理路径名。
我们平时敲的pwd命令,默认是带-L参数的。

在我的工作目录下有个vimplugin目录,我们建立到vimplugin的一个软链接tovimplugin,然后cd到tovimplugin, 分别执行pwd、pwd -L、pwd -P,就可以看出输出的区别了。
pwd和pwd -L输出的是目录的软连接名称,而pwd -P输出的是实际的物理目录名称:

              peter@www.initroot.com:~/Desktop$ pwd
                /home/peter/Desktop
              peter@www.initroot.com:~/Desktop$ ln -s vimplugin tovimplugin
              peter@www.initroot.com:~/Desktop$ ls -al
                total 1087400
                drwxr-xr-x  8 peter peter      4096 Oct 10 11:42  .
                drwxr-xr-x 38 peter peter      4096 Oct  9 23:06  ..
                drwxr-xr-x  8 peter peter      4096 Oct  6 21:14  linuxsource
                -rwxrwx---  1 peter peter 708331467 Jun 19 16:56  lnmp1.6-full.tar.gz
                -rw-rw-r--  1 peter peter     10569 Jul 30 23:06  sitemap.txt
                lrwxrwxrwx  1 peter peter         9 Oct 10 11:42  tovimplugin -> vimplugin
                drwxrwxr-x  5 peter peter      4096 Oct 10 11:41  vimplugin
              peter@www.initroot.com:~/Desktop$ cd tovimplugin
              peter@www.initroot.com:~/Desktop/tovimplugin$ pwd
                /home/peter/Desktop/tovimplugin
              peter@www.initroot.com:~/Desktop/tovimplugin$ pwd -L>
              /home/peter/Desktop/tovimplugin
              peter@www.initroot.com:~/Desktop/tovimplugin$ pwd -P
                /home/peter/Desktop/vimplugin
            

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读