Command Overview
pwd is a fundamental yet important command in Linux/Unix systems, which stands for Print Working Directory. Its core function is to display the current directory path where the user is located, helping users quickly confirm their position in the file system.
Usage
The syntax of the pwd command is as follows
pwd [options]
Common options for the pwd command and their descriptions are as follows
|
Option |
Description |
|
-L |
Display the logical path (default option, follows symbolic links) |
|
-P |
Display the physical path (ignores symbolic links, shows the real location) |
Command Case Scenarios
1. Confirm Current Location
Before operating on files, use pwd to confirm the current directory to avoid mistakes:
[root@blog ~]# pwd
/root
2. Debugging Symbolic Link Issues
When symbolic links point to nested directories, quickly verify the actual path:
# Switch to /bin directory
[root@blog ~]# cd /bin
# Display logical path (preserve link structure)
[root@blog bin]# pwd -L
/bin
# Display physical path (jump to actual location)
[root@blog bin]# pwd -P
/usr/bin
3. Advanced Applications in Script Programming
Dynamic Path Retrieval
# Store the current path in a script for subsequent automation
#!/bin/bash
current_dir=$(pwd) # Assign the current path to a variable
echo "Current script running directory: $current_dir"
# Example output
Current script running directory: /home/user/scripts
Path Concatenation and Operations
# Generate absolute path from relative path or perform cross-directory operations
file_path="$(pwd)/data.txt" # Generate file path in the current directory
cp "$file_path" /backup # Copy file to backup directory
Notes
- Symbolic Links: Understanding the difference between -L (logical path, default) and -P (physical path) is crucial for handling symbolic links.
- No Parameters: The pwd command itself usually does not require any parameters to perform its main function. Options are mainly used for special cases involving symbolic links.
- Special Character Handling: In scripts, path variables should be enclosed in quotes (e.g., current_dir=”$(pwd)”).
Recommended Reading
1. Linux Series Articles
Installing RHEL8.x Operating System
Installing RHEL9.x Operating System
Simple Configuration of RHEL9.X
2. Related Software and Images
Benefits 2 – Related Software and Images
Related Software and Images
What technical points and content would you like to see?
You can leave a message below to tell the author.
Click the bottom left corner “Read the Original” to receive benefits and download links for images.