The PWD (a.k.a. “present working directory”) will print the path of the directory you are currently working in, starting from the root directory to the current directory.
Tutorial Details
Description | PWD (Present Working Directory) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | pwd |
Internet Required | No |
Syntax of the PWD Command
The PWD command requires only one flag, and that is optional.
$ pwd [OPTION]
The PWD command recognizes the following two options:
Flags | Description |
---|---|
-L | Display the value assigned to the $PWD variable. |
-P | Display the physical directory without any symbolic links. |
By default, PWD without any flag takes the “-L
” flag in action to display the current working directory.
Printing the Current Directory Path
The following command will print the absolute path of the directory you are working in, from the root directory to the current working directory.
$ pwd
# OR
$ pwd -L
Output:
If you are working inside the symbolic directory, then the PWD command will print the symbolic link where you are currently working.
~/Documents/sampledir$ pwd
#OR
~/Documents/sampledir$ pwd -L
Output:
Printing the Actual Directory Path (Ignoring Symbolic Links)
If you want to print the actual symbolic link path where it is pointing, use the “-P
” flag.
~/Documents/sampledir$ pwd -P
Output:
The “~/Documents/sampledir
” is the symbolic link to the “/home/linuxtldr/sampledir
” directory.
Finding the PWD Path
Execute the following command to print all the locations having the executable name “pwd
” using the type command.
$ type -a pwd
Output:
Taking the /bin/pwd in Action
You can use the PWD command to print the current working directory, or the “/bin/pwd
” command. However, both are a little bit different from each other.
For example, by default, the PWD command uses the “-L
” flag to print the current working directory, but in the case of the “/bin/pwd
” command, it uses the “-P
” flag by default to print the path of the directory that the symbolic link points towards.
~/Documents/sampledir$ pwd
#AND
~/Documents/sampledir$ /bin/pwd
Output:
And you can specify the “-P
” flag separately for the PWD command; the “/bin/pwd
” command requires the “-L
” flag to print the current working directory.
~/Documents/sampledir$ pwd -P
#OR
~/Documents/sampledir$ /bin/pwd -L
Output:
Echo $PWD Variable
The PWD command uses the $PWD environment variable to store the path of the current working directory, working like the “-L
” flag.
~/Documents/sampledir$ echo $PWD
Output:
Printing the $PWD and $OLDPWD Variables
It will print the current and previous working directories as shown.
~/Documents/sampledir$ echo $PWD $OLDPWD
Output:
That’s all about the PWD command. If you have a query related to this topic, feel free to discuss it in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.