Comprehensive Guide to Linux Commands – pwd Command

Click the blue text to follow us

Comprehensive Guide to Linux Commands - pwd Command

1. Introduction

The pwd (print working directory) command is used to display the full path of the current user’s working directory.

Executing the pwd command allows you to know the absolute path name of your current working directory, facilitating subsequent work.

2. Syntax

Syntax: pwd [OPTION]

Parameter Description:

Option

Description

-L

Display the logical path (default behavior), resolving symbolic links

-P

Display the physical path, without resolving symbolic links

–version

Display version information

–help

Display help information

Working Principle:

The pwd command determines the current working directory in the following ways:

1. Check the value of the environment variable PWD (when using the -L option).

2. Or determine the path physically by traversing the parent directories (when using the -P option).

3. Practical Examples

1. View the current working directory

[root@app01 soft]# pwd
/app/soft

2. Use the -p option to view the working directory

When you are in a directory that is a symbolic link, such as a soft link directory

[root@app01 app]# ln -sv /app/soft ./soft_tmp
"./soft_tmp" -> "/app/soft"
[root@app01 app]# cd soft_tmp
[root@app01 soft_tmp]# pwd
/app/soft_tmp                         // Display logical directory
[root@app01 soft_tmp]# pwd -P
/app/soft                            // Display real physical directory

3. Using in shell scripts

When writing shell scripts, pwd is often used to get the current directory

#!/bin/bash
CURRENT_DIR=$(pwd)
echo "Current directory is: $CURRENT_DIR"

4. Common Questions and Answers

1. What is the difference between pwd and echo $PWD?

pwd is a built-in command in the Linux operating system, while $PWD is a defined environment variable value. In most cases, they output the same result, but the pwd command can take options (like -P), while $PWD always shows the logical path.

2. Why is the -P option needed?

When you are working in a directory that is a symbolic link (such as a soft link), sometimes you need to know the actual physical location rather than the symbolic link path, which is when the -P option is useful.

3. Can the pwd command fail?

Under normal circumstances, executing pwd will not fail, but if the current directory is deleted in another terminal session, executing the pwd command may fail.

Long press the QR code to follow us

Comprehensive Guide to Linux Commands - pwd CommandComprehensive Guide to Linux Commands - pwd Command

CSDN BlogδΈ¨Countless Stars in the Sky

https://blog.csdn.net/myself88129

Leave a Comment