translated

This commit is contained in:
geekpi 2014-11-29 06:15:26 -05:00
parent 8858878008
commit cf408257c1

View File

@ -1,54 +1,51 @@
Transalting-----geekpi
15 pwd (Print Working Directory) Command Examples in Linux
15条Linux下的pwd命令打印工作目录示例
================================================================================
For those working with Linux command Line, command **pwd** is very helpful, which tells where you are in which directory, starting from the root (**/**). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command **pwd** comes to rescue.
对于那些使用Linux命令行的人来说**pwd**‘命令是非常有用的,它告诉你你现在在那个目录,从根目录(**/**)开始。特别对于或许会在目录的切换间容易糊涂的Linux新手而言**pwd** 可以拯救他们。
![15 pwd Command Examples](http://www.tecmint.com/wp-content/uploads/2014/11/pwd-command.png)
15 pwd Command Examples
15 pwd 命令示例
### What is pwd? ###
### 什么是pwd? ###
**pwd** stands for **Print Working Directory**. As the name states, command **pwd** prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (**/**). This command is built in shell command and is available on most of the shell bash, Bourne shell, ksh,zsh, etc.
**pwd** 代表的是‘**Print Working Directory**’(打印当前目录)。如它的名字那样,‘**pwd**会打印出当前工作目录或仅是目录用户。它会打印出以root (**/**)为起始的完整目录名。这条命令是一条shell内建命令并且在大多数shell中都可以使用如bash、Bourne shellksh、zsh等等。
#### Basic syntax of pwd: ####
#### pwd的基本语法: ####
# pwd [OPTION]
#### Options used with pwd ####
#### pwd的选项 ####
<table border="0" cellspacing="0">
<colgroup width="126"></colgroup>
<colgroup width="450"></colgroup>
<tbody>
<tr>
<td height="21" align="LEFT" style="border: 1px solid #000000;"><b><span style="font-size: small;">&nbsp;Options</span></b></td>
<td align="LEFT" style="border: 1px solid #000000;"><b><span style="font-size: small;">&nbsp;Description</span></b></td>
<td height="21" align="LEFT" style="border: 1px solid #000000;"><b><span style="font-size: small;">&nbsp;选项</span></b></td>
<td align="LEFT" style="border: 1px solid #000000;"><b><span style="font-size: small;">&nbsp;描述</span></b></td>
</tr>
<tr>
<td height="19" align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;-L (logical)</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;Use PWD from environment, even if it contains symbolic links</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;使用环境中的路径,即使包含了符号链接</span></td>
</tr>
<tr>
<td height="19" align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;-P (physical)</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;Avoid all symbolic links</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;避免所有的符号链接</span></td>
</tr>
<tr>
<td height="19" align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;&ndash;help </span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;Display this help and exit</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;显示帮助并退出</span></td>
</tr>
<tr>
<td height="19" align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;&ndash;version</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;Output version information and exit</span></td>
<td align="LEFT" style="border: 1px solid #000000;"><span style="font-family: Liberation Serif,Times New Roman; font-size: small;">&nbsp;输出版本信息并退出</span></td>
</tr>
</tbody>
</table>
If both **-L** and **-P** options are used, option **L** is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option **-P** into account.
如果同时使用了‘**-L**‘和‘**-P****-L**会有更高的优先级。如果没有指定参数pwd会避开所有的软链接也就是说会使用**-P**‘参数。
Exit status of command pwd:
pwd的退出状态:
<table border="0" cellspacing="0">
<colgroup width="128"></colgroup>
@ -56,18 +53,18 @@ Exit status of command pwd:
<tbody>
<tr>
<td height="19" align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">0</span></td>
<td align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">Success</span></td>
<td align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">成功</span></td>
</tr>
<tr>
<td height="19" align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">Non-zero</span></td>
<td align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">Failure</span></td>
<td align="CENTER" style="border: 1px solid #000000;"><span style="font-size: small;">失败</span></td>
</tr>
</tbody>
</table>
This article aims at providing you a deep insight of Linux command **pwd** with practical examples.
本篇的目的是采用例子让你对‘**pwd**‘有更深入的领悟。
**1.** Print your current working directory.
**1.** 打印放钱工作目录.
avi@tecmint:~$ /bin/pwd
@ -75,20 +72,20 @@ This article aims at providing you a deep insight of Linux command **pwd**
![Print Working Directory](http://www.tecmint.com/wp-content/uploads/2014/11/pwd.gif)
Print Working Directory
打印工作目录
**2.** Create a symbolic link of a folder (say **/var/www/html** into your home directory as **htm**). Move to the newly created directory and print working directory with symbolic links and without symbolic links.
**2.** 为文件夹创建一个符号链接比如说在home目录下创建一个**htm**链接指向**/var/www/html**)。进入新创建的目录并打印出含有以及不含符号链接的目录。
Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.
在home目录下创建一个htm链接指向/var/www/html并进入。
avi@tecmint:~$ ln -s /var/www/html/ htm
avi@tecmint:~$ cd htm
![Create Symbolic Link](http://www.tecmint.com/wp-content/uploads/2014/11/Create-Symbolic-Link.gif)
Create Symbolic Link
创建符号链接
**3.** Print working directory from environment even if it contains symlinks.
**3.** 从当前环境中答应目录即使它含有符号链接。
avi@tecmint:~$ /bin/pwd -L
@ -96,9 +93,9 @@ Create Symbolic Link
![Print Current Working Directory](http://www.tecmint.com/wp-content/uploads/2014/11/Print-Working-Directory.gif)
Print Current Working Directory
打印工作目录
**4.** Print actual physical current working directory by resolving all symbolic links.
**4.** 解析符号链接并打印出物理目录。
avi@tecmint:~$ /bin/pwd -P
@ -106,9 +103,9 @@ Print Current Working Directory
![Print Physical Working Directory](http://www.tecmint.com/wp-content/uploads/2014/11/Print-Physical-Working-Directory.gif)
Print Physical Working Directory
打印物理工作目录
**5.** Check if the output of command “**pwd**” and “**pwd -P**” are same or not i.e., if no options are given at run-time does “**pwd**” takes option **-P** into account or not, automatically.
**5.** 查看一下“**pwd**”和“**pwd -P**”的输出是否一致,也就是说,如果没有跟上选项,“**pwd**”时候会自动采用**-P**选项。
avi@tecmint:~$ /bin/pwd
@ -116,11 +113,11 @@ Print Physical Working Directory
![Check pwd Output](http://www.tecmint.com/wp-content/uploads/2014/11/Check-pwd-Output.gif)
Check pwd Output
检查pwd输出
**Result:** Its clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “**pwd**”, it automatically takes option “**-P**” into account.
**结论:** 上面例子4和5的输出很明显结果相同当你“**pwd**”后面不带参数时pwd会使用“**-P**”选项。
**6.** Print version of your pwd command.
**6.** 打印pwd命令的版本。
avi@tecmint:~$ /bin/pwd --version
@ -134,15 +131,15 @@ Check pwd Output
![Check pwd Version](http://www.tecmint.com/wp-content/uploads/2014/11/Check-pwd-Version.gif)
Check pwd Version
检查pwd命令版本
**Note:** A pwd command is often used without options and never used with arguments.
**注意:** pwd 通常不带选项运行,且没有任何参数
**Important:** You might have noticed that we are executing the above command as “**/bin/pwd**” and not “**pwd**”.
**重要:** 你可能注意到我们刚才运行的都是 “**/bin/pwd**” 而不是 “**pwd**”。
So whats the difference? Well “**pwd**” alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using **/bin/pwd**, we are calling the binary version of that command. Both the shell and the binary version of command Prints Current Working Directory, though the binary version have more options.
这有什么区别呢?直接使用“**pwd**”意味着使用shell内置的pwd。你的shell可能有不同版本的pwd。具体请参考手册。当你使用的是**/bin/pwd**时,我们调用的是二进制版本的命令。虽然二进制的版本有更多的选项,但是它们两者都能打印当前的目录。
**7.** Print all the locations containing executable named pwd.
**7.** 打印所有含有可执行pwd的路径
avi@tecmint:~$ type -a pwd
@ -151,9 +148,9 @@ So whats the difference? Well “**pwd**” alone means shell built-in pwd. Y
![Print Executable Locations](http://www.tecmint.com/wp-content/uploads/2014/11/Print-Executable-Locations.gif)
Print Executable Locations
打印可执行文件路径
**8.** Store the value of “**pwd**” command in variable (say **a**), and print its value from the variable (important for shell scripting perspective).
**8.** 存储“**pwd**”命令的值到变量中(比如说:**a** 并从中打印i变量的值对于观察shell脚本很重要
avi@tecmint:~$ a=$(pwd)
avi@tecmint:~$ echo "Current working directory is : $a"
@ -162,11 +159,11 @@ Print Executable Locations
![Store Pwd Value in Variable](http://www.tecmint.com/wp-content/uploads/2014/11/Store-Pwd-Value-in-Variable.gif)
Store Pwd Value in Variable
存储pwd的值到变量中。
Alternatively, we can use **printf**, in the above example.
下面的例子中也可以用**printf**来替代。
**9.** Change current working directory to anything (say **/home**) and display it in command line prompt. Execute a command (say **ls**) to verify is everything is **OK**.
**9.** 将工作路径切换到其他地方(比如说 **/home**),并在命令行中显示。通过执行命令(比如说 **ls**‘)来验证一切**OK**。
avi@tecmint:~$ cd /home
avi@tecmint:~$ PS1='$pwd> ' [Notice single quotes in the example]
@ -174,14 +171,14 @@ Alternatively, we can use **printf**, in the above example.
![Change Current Working Directory](http://www.tecmint.com/wp-content/uploads/2014/11/Change-Current-Working-Directory.gif)
Change Current Working Directory
改变当前工作路径
**10.** Set multi-line command line prompt (say something like below).
**10.** 设置多行显示 (就像下面这样),
/home
123#Hello#!
And then execute a command (say **ls**) to check is everything is **OK**.
接着执行命令(比如说 **ls**)来检验一切**OK**。
avi@tecmint:~$ PS1='
> $PWD
@ -193,9 +190,9 @@ And then execute a command (say **ls**) to check is everything is **OK**.
![Set Multi Commandline Prompt](http://www.tecmint.com/wp-content/uploads/2014/11/Set-Multi-Commandline-Prompt.gif)
Set Multi Commandline Prompt
设置多行显示
**11.** Check the current working directory and previous working directory in one GO!
**11.** 一下子检查当前工作路径以及先前的工作路径。
avi@tecmint:~$ echo “$PWD $OLDPWD”
@ -204,28 +201,29 @@ Set Multi Commandline Prompt
![Check Present Previous Working Directory](http://www.tecmint.com/wp-content/uploads/2014/11/Check-Present-Previous-Working-Directory.gif)
Check Present Previous Working Directory
检查当前工作路径
**12.** What is the absolute path (starting from **/**) of the pwd binary file.
**12.** pwd文件的绝对路径以**/**开始)。
/bin/pwd
**13.** What is the absolute path (starting from **/**) of the pwd source file.
**13.** pwd源文件文件的绝对路径以**/**开始)。
/usr/include/pwd.h
**14.** Print the absolute path (starting from **/**) of the pwd manual pages file.
**13.** 打印pwd手册的绝对路径以**/**开始)。
/usr/share/man/man1/pwd.1.gz
**15.** Write a shell script analyses current directory (say **tecmint**) in your home directory. If you are under directory **tecmint** it output “**Well! You are in tecmint directory**” and then print “**Good Bye**” else create a directory **tecmint** under your home directory and ask you to cd to it.
**15.** 写一个shell脚本分析home目录下的一个目录比如**tecmint**)。如果当前目录是**tecmint**就输出“**Well! You are in tecmint directory**”接着输出“**Good Bye**”,不然就在**tecmint**下面创建一个目录并提示你cd进入它。
Lets first create a tecmint directory, under it create a following shell script file with name pwd.sh.
让我们首先创建一个tecmint目录在下面创建一个名为pwd.sh的脚本文件。
avi@tecmint:~$ mkdir tecmint
avi@tecmint:~$ cd tecmint
avi@tecmint:~$ nano pwd.sh
Next, add the following script to the pwd.sh file.
接下来在pwd.sh中加入下面的脚本。
#!/bin/bash
@ -243,7 +241,7 @@ Next, add the following script to the pwd.sh file.
}
fi
Give execute permission and run it.
给予他执行权限并运行。
avi@tecmint:~$ chmod 755 pwd.sh
avi@tecmint:~$ ./pwd.sh
@ -251,16 +249,16 @@ Give execute permission and run it.
Well you are in tecmint directory
Good Bye
#### Conclusion ####
#### 总结 ####
**pwd** is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. Thats all for now. Ill be here again with another interesting article soon, till then stay tuned and connected to Tecmint.
**pwd**是一个最简单且会广泛用到的命令。掌握好pwd是使用Linux终端的基础。就是这些了。我很快会再带来另外有趣的注意请不要走开继续关注Tecmint。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/pwd-command-examples/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出