How to Display the Current Working Directory in Linux Using the pwd Command
Introduction
The pwd
command is a Linux utility used to display the working directory to enable smooth navigation in your file system. It's also known as print working directory (pwd
) and enables filesystem navigation, script, file, and directory management.
This article explains how to use the pwd
command in Linux to manage your working directory environment.
pwd
Command Syntax
Below is the basic pwd
command syntax:
$ pwd [options]
Within the above command, pwd
outputs the full working directory path with specific filtering options.
pwd
Command Options
Option | Description |
---|---|
-L |
Display the logical working directory (default). |
-P |
Display the physical working directory without symbolic links. |
Understanding Logical and Physical Paths
- Logical Path (
-L
): Displays the working directory path as it appears in your terminal environment including any symbolic links. - Physical Path (
-P
): Displays the working directory's physical location on the filesystem and resolves symbolic links to their real locations.
Practical Examples of the pwd
Command
View the active working directory.
console$ pwd
The above command outputs your full working directory path.
Output:
Display the logical working directory.
console$ pwd -L
The above command displays the logical working directory path including any symbolic links.
Output:
Display the physical working directory path.
console$ pwd -P
The above command outputs the full working directory path and resolves any symbolic links to display the directory structure on the filesystem.
Output:
Practical Scenarios for Using pwd
Capturing the current working directory (pwd
) is essential in shell scripting to enable the creation of relative paths or set up of the script's working directory. Follow the steps below to use pwd
in a script.
Create a new sample script file
my_script.sh
using a text editor such as Nanoconsole$ nano my_script.sh
Add the following contents to the file.
bash#!/bin/bash current_dir=$(pwd) echo "The script is running in: $current_dir"
Enable execute permissions on the script.
console$ sudo chmod +x my_script.sh
Run the script
console$ bash my_script.sh
Output:
Run the following command to use
pwd
while switching directories and verify the active position in your file system.console$ cd /home/user/documents && pwd
Output:
/home/user/documents
The above command switches to the
/home/user/documents
directory and prints the working directory to verify the active file system position.
Advanced Usage Scenarios
Combine
pwd
withcd
to enable directory navigation.console$ cd /tmp && pwd
The above command changes the working directory to
/tmp
and displays the active directory.Output:
Use
pwd
withls
.console$ pwd && ls
The above command displays the working directory and lists all included contents.
Output:
Store the working directory in a variable and view the result using
echo
.console$ current_dir=$(pwd) && echo "You are currently in: $current_dir"
Output:
Combine
pwd
withfind
to search search files.console$ find $(pwd) -name "*.log"
The above command searches for all files with the
.log
extension in the active working directory.Output:
Use the
pwd
command in error handling.console$ if [ ! -d "/some/directory" ]; then echo "Directory not found, current location is: $(pwd)" fi
The above command displays the working directory result to enable debugging in a scripts.
Output:
Use
pwd
in automation scripts.Create a new sample script file
pwd_scrpt.sh
using a text editor such as Nanoconsole$ nano pwd_script.sh
Add the following contents to the file.
bash#!/bin/bash if [ $(pwd) != "/expected/directory" ]; then echo "Please navigate to /expected/directory before running this script." exit 1 fi
Save and close the file.
Enable execute permissions on the file.
console$ chmod +x pwd_script.sh
Run the script
console$ bash pwd_script.sh
The above bash script automates tasks that depend on the active working directory to run perform a specific function.
Output:
Conclusion
You have used the pwd
command to view your working directory, navigate the filesystem, and write effective scripts. For more command options, run the man pwd
command to view the pwd
manual page.