Beginners Guide for PWD Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

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

DescriptionPWD (Present Working Directory)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitespwd
Internet RequiredNo

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:

FlagsDescription
-LDisplay the value assigned to the $PWD variable.
-PDisplay 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:

Checking the current working directory

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:

Checking the symbolic link directory

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:

Printing the actual directory path

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:

Printing the path of the pwd executable file

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:

Checking the current path using pwd and its binary file

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:

Checking the current path using pwd and its binary file with the flag

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:

Checking the PWD environmental variable

Printing the $PWD and $OLDPWD Variables

It will print the current and previous working directories as shown.

~/Documents/sampledir$ echo $PWD $OLDPWD

Output:

Checking the PWD and OLDPWD environmental variables

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.