Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the pwd command in linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux system how to use the pwd command, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Use the pwd command in Linux to see the full path to the "current working directory". To put it simply, every time you operate on the terminal, you will have a current working directory.

1. Command format:

Pwd [options]

2. Command function:

View the full path to the current working directory

3. Common parameters:

Normally, there are no parameters.

If the directory is a link:

Format: pwd-P shows the actual path instead of using the link path.

4. Common examples:

Example 1: use the pwd command to view the full path of the default working directory

Command:

Pwd

Output:

The code is as follows:

[root@localhost ~] # pwd

/ root

[root@localhost ~] #

Example 2: use the pwd command to view the specified folder

Command:

Pwd

Output:

The code is as follows:

[root@localhost ~] # cd / opt/soft/

[root@localhost soft] # pwd

/ opt/soft

[root@localhost soft] #

Example 3: when a directory connects to a link, pwd-P shows the actual path instead of using the link path; pwd shows the connection path

Command:

Pwd-P

Output:

The 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 [options]

Options:

When the-L directory connects to the link, output the connection path

-P output physical path

Output:

The 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] #

Example 5: the current directory is deleted, but the pwd command still shows that directory

Output:

The 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 introduces the detailed usage of the pwd command in linux, in addition to printing the current working directory, there are also some special uses, I hope to help you.

The pwd command on linux is an acronym for Print Working Directory. The basic function is to print the current working directory, but there are also some special uses. This article summarizes how to use pwd.

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

The code is as follows:

Www.yisu.com:~# cd / proc/self

Www.yisu.com:/proc/self# pwd

/ proc/self

Www.yisu.com:/proc/self# pwd-P

/ proc/5401

Www.yisu.com:/proc/self# ls-l / proc/self

Lrwxrwxrwx 1 root root 64 Jun 23 18:25 / proc/self-> 5410

Www.yisu.com:/proc/self#

Pwd: displays the current work path.

Pwd-P: if the current work path is a link, display the original path of the link, that is, the actual path, as shown in ls-l / proc/self.

Pwd-L: displays the link path as opposed to pwd-P

Let's talk about the environment variables PWD and OLDPWD in the linux system again.

Environment variable PWD: the environment variable that displays the current work path

Environment variable OLDPWD: displays the last work path.

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

The code is as follows:

Www.yisu.com:~# cd / var/www

Www.yisu.com:/var/www# echo $PWD

/ var/www

Www.yisu.com:/var/www# echo $OLDPWD

/ root

Www.yisu.com:/var/www#

It's easier to understand.

Attached to get the code of the parent directory of the current directory:

The code is as follows:

Www.yisu.com:/var/www# echo $(dirname $(pwd))

/ var

Www.yisu.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. Take a look at the difference through the following example:

The code is as follows:

Www.yisu.com:/var/www# pwd-P

/ var/www

Www.yisu.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 command at all, so the problem arises: what is the connection and difference between the two commands?

Take a look at these examples:

The code is as follows:

Www.yisu.com:/var/www# cd / /

Www.yisu.com://# pwd

/ /

Www.yisu.com://# / bin/pwd

/

Www.yisu.com://#

Thus, the basic functions of the two are the same, but / bin/pwd is more accurate when displaying the current directory, while pwd is easily fooled by the operator. But there's not much difference.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report