1. Overview
The pwd (Print Working Directory) command is a Linux tool that displays the current directory’s pathname, thereby aiding navigation, scripting, file management, and task automation. It’s particularly useful when navigating complex directory structures.
In this tutorial, we’ll analyze the functions of the pwd command and its syntax, usage, and options. Likewise, we’ll offer some practical examples to illustrate its usage scenarios under Linux, providing a comprehensive guide.
2. Understanding the pwd command
To begin with, let’s understand the pwd command. So, this command outputs the full path from the root directory (/) to the current directory by displaying the absolute pathname of the current working directory.
Now, we can examine it’s basic syntax:
pwd [OPTIONS]
Now that we’ve seen the syntax, let’s understand these parameters:
- [OPTIONS]: flags that modify the command’s behavior and output.
Note that pwd prints the complete path to the current working directory without any parameters.
Here’s an example:
$ pwd
/home/gbenga
The output from the pwd command is the absolute path starting from the root directory (/), clearly displaying the hierarchical structure of directories.
For example, consider the output /home/kali. Here, / represents the root directory, followed by home, a subdirectory under the root, and finally, Kali, a subdirectory under home.
Let’s see a breakdown of the command’s options and their functions:
Options
Description
-L, –logical
Displays the logical current directory, including any symbolic links.
-P, –physical
Displays the physical current directory by resolving all symbolic links.
Next, we proceed to apply this command in practice.
3. Basic Usage
We’ve previously demonstrated the basic usage of this command. However, using the pwd command by itself and with the -L option yields the same result.
Let’s compare the command syntax by itself and with the -L option on the newly created directory inside the Documents directory. Initially, we changed the directory (cd) into the new directory named baeldung_dir, then applied the commands:
$ pwd
/home/gbenga/Documents/baeldung_dir
Next, we examine the addition of the -L option to the pwd command:
$ pwd -L
/home/gbenga/Documents/baeldung_dir
As shown in both results above, they’re identical. So, the default command output is the same as when used with the -L option.
The output shows baeldung_dir as the current working directory and states the full pathname from the root to this directory.
3.1. Finding Logical Path With Option -L
In addition to the details discussed earlier about option -L, this option prompts the pwd command to show a logical path. Meaning it follows any symbolic links (symlinks) in the path.
Let’s demonstrate how symbolic links work and how the pwd command handles it.
Using the ln -s command, we can create an actual directory (~/Desktop/real_dir) and a symbolic link (~/Desktop/symlink_dir) that points to the actual directory (real_dir):
$ mkdir -p ~/Desktop/real_dir
$ sudo ls -s ~/Desktop/real_dir ~/Desktop/symlink_dir
As shown above, we ran two consecutive commands. Firstly, we created a directory named real_dir inside the Desktop directory. Secondly, we used the ln -s command to create a symbolic link named symlink_dir for the real_dir directory.
Now, we can proceed to check the output of the logical path option on real_dir and symlink_dir:
$ cd ~/Desktop/real_dir
$ pwd -L
/home/gbenga/Desktop/real_dir
$ cd ~/Desktop/symlink_dir
$ pwd -L
/home/gbenga/Desktop/symlink_dir
The result above proves that we’re in the real_dir or symlink_real directory, and the pwd -L outputs the full pathname to the current directory.
In the next section, let’s examine the behavior of pwd -P and analyze its output.
3.2. Finding Physical Path With Option -P
Let’s repeat the last commands in the previous section but with the -P option instead:
$ cd ~/Desktop/real_dir
$ pwd -P
/home/gbenga/Desktop/real_dir
$ cd ~/Desktop/symlink_dir
$ pwd -P
/home/gbenga/Desktop/real_dir
Critical note: the pwd -P resolves the symbolic link, symlink_dir, back to the full pathname for the real_dir, which is /home/gbenga/Desktop/real_dir.
Consequently, we can infer that the pwd -P displays the full pathname of the actual directory when applied to both the real directory and symbolic link directory.
4. Advanced Uses of pwd
After understanding the primary usage of this command, we can proceed to advance our knowledge.
4.1. Saving Current Directory When Running Script
For example, in some cases, the configuration must append the current directory’s absolute path. So, this can easily be achieved using pwd within the script:
#!/bin/bash
# Get the current working directory
current_dir=$(pwd)
# Configuration file path
config_file="$current_dir/config.txt"
# Append current directory to the configuration file
echo "path=$current_dir" >> "$config_file"
echo "Configuration updated with path $current_dir"
In the script above, pwd provides the full pathname to the current working directory of whatever directory the script is executed. Furthermore, the $current_dir creates a file by appending /config.txt.
Finally, the current pathname is appended to config_file, which creates config.txt in the current directory. Let’s check the output of this script when it’s executed:
$ bash confile.sh
Configuration updated with path /home/gbenga/Documents
Next, we can list (ls) the content of the current directory and cat the config.txt:
$ ls
baeldung_dir config.txt confile.sh new_dir symLink_to_baeldung_sir
$ cat config.txt
path=/home/gbenga/Documents
The result shows the current path appended to the config.txt file.
4.2. Setting Environmental Variable Using pwd
Another usefulness of pwd is that it could be used to set the current working directory as an environmental variable.
Let’s examine it by including it in a Bash script that creates an environment variable:
#!/bin/bash
# Set an environment variable to the current directory
export PROJECT_DIR=$(pwd)
# Use the environment variable
echo "Project directory is set to $PROJECT_DIR"
# Create a new directory inside the current directory
mkdir -p "$PROJECT_DIR/new_folder"
The script sets the environment variable PROJECT_DIR to the path of the current working directory. Subsequently, it prints this path and creates a new directory named new_folder within the current directory.
This is useful for dynamically setting paths and creating directories based on the current working location.
Let’s examine the output of the script:
$ source project_dir.sh
Project directory is set to /home/gbenga/Documents
$ echo $PROJECT_DIR
/home/gbenga/Documents
By using the source command, we implement the project_dir.sh script in the current shell sets the PROJECT_DIR environment variable to the current directory.
Subsequently, the echo $PROJECT_DIR command confirms that this environment variable is set correctly and available in the current shell session.
5. Conclusion
In this article, we examine the function of the pwd command and, more importantly, its options. Furthermore, we covered clear and concise explanations of the command’s prompts and application.
In addition, We introduced a few other advanced ways to use this command, including its combination with different commands.