如何在Linux/UNIX中使用pwd命令

时间:2020-01-09 10:43:06  来源:igfitidea点击:

如何在Linux或者Unix等操作系统上使用pwd命令?
如何在UNIX或者Linux Shell脚本中使用pwd命令实现自动化?

" pwd"是printworkingdirectory的首字母缩写。

pwd命令与ls和cd命令一起被认为是Linux,AIX,HP-UX,* BSD和其他UNIX等操作系统上最常用的命令之一。

当前目录

当前目录不过是使用bash或者ksh或者zsh或者tcsh/csh shell时当前正在操作的目录。
您需要打开终端(GUI)或者在控制台上登录才能使用命令行。
语法为:

语法

要打印当前工作目录,请执行:

pwd
pwd [options]
var=$(pwd)
echo "The current working directory $var."

例子

输出示例:

$ pwd

在此示例中,"/home/Hyman"是当前目录。
在Unix之类的操作系统下,任何目录的完整路径始终以正斜杠表示统计信息。
简而言之:

/home/Hyman

要将当前目录存储在名为x的shell变量中,请执行:

  • /正斜杠系统或者文件系统上的根目录。
  • home子目录
  • Hyman子目录

要打印当前目录,请使用printf命令或者echo命令:

x=$(pwd)

或者

echo "The current working directory : $x"

大多数Unix用户将pwd命令与ls和cd命令一起使用:

printf "The current working directory : %s" $x

使用pwd的典型Linux/Unix shell会话

输出示例:
使用pwd,ls和cd命令的典型Shell用户会话。

## Where am I?
pwd
 
## List the contents of the current directory
ls
ls -l
 
# Change the current directory to Videos
cd Videos
pwd

您的Shell可能有其自己的pwd版本,通常会取代以下描述的版本。
要查看所有包含名为pwd的可执行文件的位置,请执行:

在以上示例中,pwd命令用于确认当前目录已被实际更改。

壳牌密码vs/bin/pwd

输出示例:

$ type -a pwd

通过输入pwd,您最终将使用bash或者ksh提供的内置shell:

pwd is a shell builtin
pwd is /bin/pwd

要使用二进制版本,请输入完整路径/bin/pwd:

pwd

请注意,这两个命令都将打印当前目录/工作目录。
但是,/bin/pwd如下所述具有更多选项。

/bin/pwd

要显示逻辑当前工作目录,请执行:

密码选项

-L选项使pwd使用来自环境的$PWD,即使它包含符号链接。
如果环境变量PWD的内容提供了当前目录的绝对名称,则没有。
或者..组件,但可能带有符号链接,然后输出这些内容。
否则,请使用默认的-P处理:

$ pwd -L

显示当前的物理工作目录(已解析所有符号链接)。
例如,~/bin /是符号链接:

$ pwd -P

输出示例:

$ pwd
$ ls -l ~/bin/

cd到~/bin /并使用pwd验证当前的工作目录:

lrwxrwxrwx 1 Hyman Hyman 35 Jan 13  2012 /home/Hyman/bin -> /home/Hyman/realdata/scripts/utils/

输出示例:

$ cd ~/bin/
$ pwd

要查看实际的当前物理工作目录并避免使用所有名为/home/Hyman/bin的符号链接,请执行:

/home/Hyman/bin

输出示例:

$ pwd -P

pwd命令的/bin/pwd版本还有两个追加选项。
要显示pwd命令版本,请执行:

/home/Hyman/realdata/scripts/utils

/bin/pwd选项

输出示例:

$ /bin/pwd --version

要查看有关pwd的信息,请执行:

pwd (GNU coreutils) 8.5
Copyright (C) 2010 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 --help

基本版本:

Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.
 
  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit
 
NOTE: your shell Jan have its own version of pwd, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.
 
Report pwd bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'pwd invocation'

Shell脚本示例

一个完整的工作示例,在设置目录权限之前,该示例使用pwd命令通知用户当前的工作目录。

#!/bin/bash
## Get the working dir
_d="$(pwd)"
 
## cd to target
cd /nas03/theitroad/images/today
 
## do something
echo "Uploading data to cdn..."
 
## get back to old dir
cd "$_d"

关于bash/ksh工作目录shell变量的注释

bash和ksh(以及其他shell)在使用cd命令时设置了以下环境变量:

  • OLDPWD以前的工作目录,由cd命令设置。
  • PWD由cd命令设置的当前工作目录。

要打印环境变量,请执行:

$ echo "$PWD $OLDPWD"

要使用环境变量,请执行:

$ pwd
/home/accounts/office/b/bom/f/2/2008/10/
$ cd /home/sales/pdfs
$ ls
$ vi sample.broacher.txt
# go back to /home/accounts/office/b/bom/f/2/2008/10/
$ cd "$OLDPWD"
$ pwd