Usage of pwd commands in linux

Source: Internet
Author: User
On linux, the pwd command is short for PrintWorkingDirectory. The basic function is to print the current working directory, but it also has some special usage. This article summarizes the usage of pwd.

In Linux, run the pwd command to view the complete path of the "current working directory. To put it simply, whenever you operate on a terminal, you will have a current working directory.

If you are not sure about the current location, pwd is used to determine the exact location of the current directory in the file system.

1. command format:

Pwd [option]

2. command functions:

View the complete path of the "current working directory"

3. common parameters:

Generally, no parameters are included.
If the directory is linked:
Format: pwd-P shows the actual path instead of the link path.

4. common instances:

Instance 1: run the pwd command to view the full path of the default working directory.

Command:
Pwd
Output:


Copy codeThe code is as follows:
[Root @ localhost ~] # Pwd
/Root
[Root @ localhost ~] #

Instance 2: Use the pwd command to view the specified folder
Command:
Pwd
Output:


Copy codeThe code is as follows:
[Root @ localhost ~] # Cd/opt/soft/
[Root @ localhost soft] # pwd
/Opt/soft
[Root @ localhost soft] #

Example 3: pwd-P shows the actual path instead of the link path when connecting to the directory. pwd shows the connection path.

Command:
Pwd-P
Output:


Copy codeThe code is as follows:
[Root @ localhost soft] # cd/etc/init. d
[Root @ localhost init. d] # pwd
/Etc/init. d
[Root @ localhost init. d] # pwd-P
/Etc/rc. d/init. d
[Root @ localhost init. d] #

Instance 4:/bin/pwd

Command:
/Bin/pwd [option]
Option:
-L directory connection path output
-P physical output path
Output:


Copy codeThe code is as follows:
[Root @ localhost init. d] #/bin/pwd
/Etc/rc. d/init. d
[Root @ localhost init. d] #/bin/pwd -- help
[Root @ localhost init. d] #/bin/pwd-P
/Etc/rc. d/init. d
[Root @ localhost init. d] #/bin/pwd-L
/Etc/init. d
[Root @ localhost init. d] #

Instance 5: the current directory is deleted, and the pwd command still displays the Directory

Output:

 
Copy codeThe code is as follows:
[Root @ localhost init. d] # cd/opt/soft
[Root @ localhost soft] # mkdir removed
[Root @ localhost soft] # cd removed/
[Root @ localhost removed] # pwd
/Opt/soft/removed
[Root @ localhost removed] # rm ../removed-rf
[Root @ localhost removed] # pwd
/Opt/soft/removed
[Root @ localhost removed] #/bin/pwd
/Bin/pwd: couldn't find directory entry in ".." with matching I-node
[Root @ localhost removed] # cd
[Root @ localhost ~] # Pwd
/Root
[Root @ localhost ~] #


The following is a supplement:

This article describes the usage of the pwd command in linux. in addition to printing the current working directory, it also has some special usage, which is helpful to you.
On linux, the pwd command is short for Print Working Directory. The basic function is to Print the current Working Directory, but it also has some special usage. This article summarizes the usage of pwd.

Let's take a look at the pwd without parameters, and then introduce the role of-P, for example:


Copy codeThe code is as follows:
Www.bitsCN.com :~ # Cd/proc/self
Www.bitsCN.com:/proc/self # pwd
/Proc/self
Www.bitsCN.com:/proc/self # pwd-P
/Proc/5401.
Www.bitsCN.com:/proc/self # ls-l/proc/self
Lrwxrwxrwx 1 root 64 Jun 23 18:25/proc/self-> 5410
Www.bitsCN.com:/proc/self #

Pwd: displays the current working path.
Pwd-P: if the current working path is a link, the original path of the link is displayed, that is, the actual path, as shown in ls-l/proc/self.
Pwd-L: displays the link path, which is the opposite of pwd-P.

Next we will talk about the environment variables PWD and OLDPWD in linux.
Environment variable PWD: displays the environment variable of the current working path

Environment variable OLDPWD: displays the last working path.

Note: Because it is an environment variable, you need to add the $ variable flag when using it. the following command demonstrates the usage of this environment variable in detail.
 

Copy codeThe code is as follows:
Www.bitsCN.com :~ # Cd/var/www
Www.bitsCN.com:/var/www # echo $ PWD
/Var/www
Www.bitsCN.com:/var/www # echo $ OLDPWD
/Root
Www.bitsCN.com:/var/www #

It is easy to understand.

To obtain the code of the parent directory of the current directory:
 


Copy codeThe code is as follows:
Www.bitsCN.com:/var/www # echo $ (dirname $ (pwd ))
/Var
Www.bitsCN.com:/var/www #

Built-in command pwd and external command/bin/pwd.
The built-in command pwd and the external command/bin/pwd are two different commands. the following example shows the differences:
 

Copy codeThe code is as follows:
Www.bitsCN.com:/var/www # pwd-P
/Var/www
Www.bitsCN.com:/var/www #/bin/pwd-P
/Bin/pwd: invalid option -- P
Try '/bin/pwd -- help' for more information.

It can be seen that the built-in command pwd and the external command/bin/pwd are not the same at all. the problem arises. What are the contact points of these two commands?
Let's look at these examples:
 

Copy codeThe code is as follows:
Www.bitsCN.com:/var/www # cd //
Www.bitsCN.com: // # pwd
//
Www.bitsCN.com: // #/bin/pwd
/
Www.bitsCN.com ://#

It can be seen that the basic functions of the two are the same, but when the current directory is displayed,/bin/pwd is more accurate, and pwd is easily fooled by the operator. But the difference is not big.

This article introduces how to use the pwd command in linux.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.