The PWD is a short form of โPrint/Present Working Directoryโ that is used to print the absolute path of the current directory. In most Linux distros, you may get a hint of the current directory from the terminal. However, this โpwdโ utility is quite useful, especially when you do not have any hint about the exact path of the directory. Considering its importance, this guide will demonstrate the working and usage of the โpwd” command with various examples in Linux.
The content carried out by this guideline is as follows:
- How Does pwd Command Work?
- How to Use the pwd Command in Linux?
- Example 1: Find the Path of the Working Directory
- Example 2: Print Physical Directory
- Example 3: Print the Value of $PWD
Let’s start this article.
How Does pwd Command Work?
Most network administrators use the โpwdโ command to invoke the complete path of the current or previous active (working) directory. The syntax of the โpwdโ command is stated below:
Syntax:
$ pwd <options>
Options:
Different options for the โpwdโ command are explained as below:
options | Description |
-p (physical) | This option refers to the actual path. |
-l (logical) | It prints out the symbolic path. |
-v (version) | It represents the current version of pwd |
–help | It displays all the helpful information regarding pwd. |
To explore more about the โpwdโ command, the โpwd –helpโ can be used as follows:
$ pwd --help
Let’s explore the usage of the pwd command.
How to Use the pwd Command in Linux?
The โpwdโ command has various practical usages, including physical and symbolic addresses. Different examples of โpwdโ commands are explained here:
Example 1: Find the Path of the Working Directory
The primary usage of the โpwdโ command can be accessed via the following command:
$ pwd
After executing the command, you can display that current directory is โhomeโ.
Example 2: Print Physical Directory
The physical path of the present directory can be obtained through the โ-Pโ option of the โpwdโ utility as follows:
$ pwd - P
It also prints the address of the present directory.
Example 3: Print the Value of $PWD
By default, the โ$PWDโ variable points towards the path of the current directory. Its value can be retrieved using the โ-Lโ option of the โpwdโ command as described below:
$ pwd - L
Letโs dig into some more usage of the โpwdโ utility.
How to Use pwd Command in Bash Scripts?
The โpwdโ command is utilized in an existing โhello.shโ script file by storing the directory information in the โxโ variable and displaying it through the โcatโ command in the terminal as below:
$ cat hello.sh
Execute the script stored in the โhello.shโ file by navigating through the below command:
$ ./hello.sh
The output shows the current directory โ/home/itslinuxfossโ, where the script file is placed.
How to Check the Previous Directory?
To find out the previous working directory, the โ$OLDPWDโ command utility is used with โechoโ as below:
$ echo "$OLDPWD"
The output returns path โ/home/itslinuxfoss/Downloadsโ on which previous work through this terminal.
How to Check the Version of pwd Utility?
To check the current version of the โpwdโ command, first, get the path of the โpwdโ utility as follows:
$ which pwd
Or the path can also be retrieved via the command:
$ type -a pwd
Now, use one of the paths with the โ— versionโ option to get the installed version of the โpwdโ utility:
$ /bin/pwd -version
The execution returns the currently installed version of pwd as โpwd (GNU coreutils) 8.32โ.
Thatโs all from this guide!
Conclusion
In Linux, the โpwdโ command prints out the directory on which you are currently working. It rescues the system users when lost in multiple directories and provides an exact path from the root to the current directory. This guide has explained the pwd command with syntax and practical usage in the Linux operating system.
Check our LinkedIn company page