pwd Command in Linux | Explained

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:

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:

optionsDescription
-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
–helpIt 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.