您的位置:首页 > 运维架构 > Linux

[一天几个linux命令] pwd--最软的柿子

2017-05-26 20:20 281 查看

文档介绍

pwd -> Print the name of the current working directory

见文知意 打印当前的工作目录

选项

选项描述英文
-L使用环境中的路径,即使包含了符号链接print the value of $PWD if it names the current working directory (Display the logical current working directory*(mac版本的)*)
-P使用物理路径print the physical directory, without any symbolic links
If no options are specified, the -L option is assumed.

如果没有指定参数,会默认的带上
-L


如果同时使用了
-L
-P
,
-L
会有更高的优先级

pwd的退出状态

状态结果
0成功
非0失败

实战

打印当前工作目录

localhost:~ jianglei$ pwd
/Users/jianglei


为文件夹创建一个符号链接(比如在home目录下创建一个htm 指向 home目录下的html目录)。进入新创建的目录并打印出含有以级不含有符号链接的目录。

在html目录下创建一个htm链接指向./html,并进入

localhost:~ jianglei$ ln -s ./html htm
localhost:~ jianglei$ ls -al | grep htm
lrwxr-xr-x    1 jianglei  staff       6  5 25 22:52 htm -> ./html
drwxr-xr-x    3 jianglei  staff     102  2 14 21:32 html
localhost:~ jianglei$ cd htm
localhost:htm jianglei$


从当前环境中打印目录即使它含有符号链接

localhost:htm jianglei$ pwd -L
/Users/jianglei/htm
localhost:htm jianglei$


解析符号链接并打印出物理目录

localhost:htm jianglei$ pwd -P
/Users/jianglei/html
localhost:htm jianglei$


查看一下
pwd
pwd -P
的输出是否一致,也就是说,如果没有跟上选项,
pwd
时会自动采用
-P
选项

localhost:htm jianglei$ pwd -L
/Users/jianglei/htm
localhost:htm jianglei$pwd
/Users/jianglei/htm
localhost:htm jianglei$


结论 :
pwd
后面不带参数时,pwd会使用
-P
选项。

打印pwd命令的版本

avi@tecmint:~$ /bin/pwd --version

pwd (GNU coreutils) 8.23
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.


重要:你可能注意到我们刚才运行的是
/bin/pwd
而不是
pwd


这有什么区别呢?

直接使用
pwd
竟味着使用shell内置的pwd。你的shell可能有不同版本的pwd。具体请参考手册。

当你使用的是/bin/pwd时,我们调用的是二进制版本的命令。虽然二进制的版本有更多的选项,但是它们两都都能打印当前的目录。

打印所有含有可执行pwd的路径

[jianglei@localhost ~]$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
pwd is /usr/bin/pwd


存储”pwd”命令的值到变量中(比如说:a),并从中打印变量的值(对于观察shell脚本很重要)

localhost:htm jianglei$ a=$(pwd)
localhost:htm jianglei$ echo "Current working directory is: $a"
Current working directory is: /Users/jianglei/htm


将工作路径切换到其他地方(比如说 /home),并在命令行中显示。通过执行命令(比如说 ‘ls‘)来验证一切OK

localhost:~ jianglei$ PS1='$pwd> '
> cd /
> ls


一下子检查当前工作路径以级先前的工作路径

localhost:/ jianglei$ echo "$PWD $OLDPWD"
/ /Users/jianglei


原文出处

详细讲解Linux系统中pwd命令的使用技巧:http://www.jb51.net/LINUXjishu/342998.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: