The pwd
(print working directory) command is one of the most basic and frequently used commands in Linux/Unix. It is used to display the current working directory in which the user is operating. This command is essential for navigation and understanding your position in the file system’s directory structure.
What is pwd
?
The pwd
command stands for Print Working Directory. It outputs the full path of the directory you are currently in, starting from the root /
directory. The working directory is the directory where your commands are executed by default.
Syntax
The pwd
command is simple, with a minimal set of options to modify its behavior.
Options in pwd
-L
or--logical
: Prints the logical path (includes symbolic links).-P
or--physical
: Prints the physical path by resolving symbolic links.
Examples of pwd
Usage
1. Display the Current Directory
Simply typing pwd
will display the full path of the directory you are in.
Command:
Output:
Here, /home/user
is the current working directory.
2. Logical vs. Physical Path
The -L
and -P
options allow you to control whether symbolic links are included in the output.
Example: Logical Path
If you navigate using symbolic links:
Output:
Example: Physical Path
When you want the actual directory path (resolving symbolic links):
Output:
3. Using pwd
in Scripts
The pwd
command is widely used in shell scripts to dynamically identify the directory in which a script is running.
Example:
Output:
4. Combining pwd
with Other Commands
Example: Save the Current Directory
You can save the output of pwd
to a variable or a file for later use:
Example: Write Current Directory to a File
Output:
5. Verify Navigation
After navigating through directories, you can use pwd
to confirm your current location.
Command:
Output:
6. pwd
in Combination with cd
You can use pwd
with the cd
command to track your movement across directories.
Command:
Output:
7. Error-Free Output
The pwd
command guarantees error-free output because it directly reads the current directory information from the shell environment.
Command:
Output:
Practical Applications of pwd
- Scripting: Often used to dynamically obtain the current directory during script execution.
- Navigation: Helps users confirm their location in a complex directory structure.
- Debugging: When working with symbolic links,
pwd -P
ensures you are aware of the physical path.
The pwd
command is a fundamental Linux/Unix command that plays a vital role in directory navigation and scripting. Its simplicity, combined with its utility, makes it an essential tool for users and administrators alike. Understanding its options (-L
and -P
) provides greater control when dealing with symbolic links, ensuring accurate and meaningful results. Whether you are a beginner or an experienced Linux user, mastering the pwd
command is essential for efficient system navigation and management.