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

文档介绍

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$ 

查看一下pwdpwd -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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值