The pwd Linux Command

December 22, 2021


The pwd Linux command prints the current working directory path, starting from the root (/). Use the pwd command to find your way in the Linux file system structure maze or to pass the working directory in a Bash script.

In this tutorial, you will learn to use the pwd command.

How to use the pwd Linux command.


  • A system running Linux
  • Access to the terminal window (Ctrl+Alt+T)
  • A text editor of your choice

pwd Syntax

The pwd command takes the following syntax:

pwd [-options]

The available options are discussed in the following section.

Exit Status

The pwd command has two exit statuses:

  • 0. Success.
  • Non-zero. Failure.

pwd Options

The options provide additional functionality to the command. The pwd command accepts the following options:

-L, --logicalInstructs pwd to output the $PWD environment variable contents, including symbolic links. If no option is specified, pwd assumes -L.
-P, --physicalPrints the path to the current directory. All the components are directory names, and symbolic links are resolved.
--versionOutputs the program version.
--helpDisplays the help message.

pwd Examples

There are several ways to use the pwd command. The examples below explain common use cases for the pwd command.

Example 1: Get Working Directory Path

Running the pwd command without any options outputs the full path to the current working directory.

For example:

An example of the pwd command output in Linux.

The command outputs the current working directory absolute path. In this case, the home directory path.

Note: An absolute pathname starts with the / character, which marks the root of the file system tree.

However, if you followed a symlink to the current directory, running pwd prints the symlink location and not its destination.

In the following example, we created a symbolic link to the current directory. Following the link and running the pwd command displays the symbolic link in the path:

An example of pwd command output that contains symbolic link location.

Example 2: Using the -P Option

The -P option instructs pwd to print the physical working directory and avoids listing symbolic links. Therefore, the command prints the absolute path to the current working directory.

For example:

This example shows how the pwd -P option works for getting the physical location of a directory.

Even though a symlink was used to change the directory, the pwd command only prints the actual name of the working directory.

Example 3: Using the -L Option

The -L option instructs pwd to print the working directory path, including any symlinks. In the following example, we used a symlink to navigate to a directory and ran pwd -L:

This example shows how the pwd -L option outputs the logical location in Linux.

The output lists the path with the symbolic link.

Example 4: Print $PWD Variable Contents

The $PWD environment variable stores the current directory path. Print the variable contents by running:

echo $PWD
Printing the $PWD variable contents in Linux.

The command outputs the current working directory, including any symlinks, stored in the $PWD variable.

Example 5: Personalize pwd Output

Personalize the pwd command output by storing its value in a variable and adding a message using the echo command.

The following example is a script where pwd's value is stored in the $p variable and includes an output message.

Follow the instructions below:

1. Open the terminal and create the script:


2. Add the following lines to the script:

echo "You are currently in: $p"

3. Save the script and exit vi with the following command:


4. Change file permissions to make the script executable:

chmod +x

5. Run the script:

Using the pwd command in a bash script to personalize the output.

The script outputs the current working directory with a personalized message.

Example 6: Check pwd Version

The pwd command is a built-in shell command (pwd) and an actual binary (/bin/pwd). The shell version may differ from the binary version.

Check which pwd version is installed by running:

/bin/pwd --version
How to check the pwd command version in Linux.

The command outputs the binary pwd version on your system.

Note: Use the type command to get more information about the pwd command. Run type -a pwd to get the locations of all executables containing the pwd keyword - both the shell and binary versions.

Example 7: See Previous Working Directory

The $OLDPWD variable stores the previous working directory path. Check your previous location in the filesystem by running:

echo "$OLDPWD"
Using the $OLDPWD variable to output the previous working directory path in Linux.

The command outputs the previous working directory.

Example 8: See Help File

Use the --help option to see the pwd command help file and the available options:

pwd --help
The help file contents for the pwd command.

The output is a brief help file with a list of available options.

Example 9: Use pwd in Scripting

In this example, we created a bash case statement that uses pwd to output the current or previous working directory, depending on the option selected. Follow the steps below:

1. Create a script using a text editor such as vi/vim:


Note: Check out how to install vi/vim on Ubuntu.

2. Enter the lines below:

echo "I need to see:"
echo "1 - My current physical directory path."
echo "2 - My current directory path, including symlinks."
echo "3 - My previous directory"
read directory
case $directory in
1) echo "Your current physical location is: $(pwd -P)";;
2) echo "Your current directory, including symlinks, is: $(pwd -L)";;
3) echo "You were previously in: $OLDPWD";;

3. Save and quit vi by running:


4. Make the script executable with chmod:

chmod +x

5. Execute the script and choose an option:


For example:

An example of using the pwd command in a bash case statement.

The script utilizes a case statement to offer three options and prints a corresponding output based on the answer.

6. Change the directory using a symlink, execute the script again and compare the outputs:

Testing the pwd command in a bash case statement.

The output shows the physical location, even though we used a symlink to change the directory.

7. Test the remaining options as well to see the result:

Testing various options for using the pwd command in a bash case statement.

Example 10: Set up an Alias for pwd

Create an alias that includes the -P option to prevent getting symlinks when running pwd. That way, pwd always shows the path to the directory you're in, regardless of how you got there.

In the following example, the alias includes the -P option and we have added it to the .bashrc file to make the system remember the alias after a system reboot.

alias pwd='pwd -P'; echo "alias pwd='pwd -P'" >> ~/.bashrc

The output displays the physical location, although we followed a symlink to the current path.


This tutorial showed how to use the pwd command in Linux to check your location quickly.

If you are interested in bash, see our tutorial for bash functions and how to use them. Alternatively, we recommend delving further into Linux commands in our list of all important Linux commands in one place.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Bash trap Command Explained
December 16, 2021

A Bash shell script can run into problems during its execution, resulting in an error signal. This tutorial will show you how to use the trap command to ensure your script always exits in a predictable manner.
Read more
How to Use Linux nohup Command
December 16, 2021

Learn how to use the nohup command to continue running commands and processes in Linux even after the shell closes.
Read more
How to Use the ulimit Linux Command
December 9, 2021

The ulimit shell command allows you to view or limit the amount of resources a user can consume. Limiting resources prevents adverse effects in the system when a user or program consumes all the available resources.
Read more
Linux set Command & How to Use it {9 Examples}
November 23, 2021

The set command allows you to show or change the shell and environment variables in a Linux system. Follow this tutorial to learn how to use the set command and see practical examples.
Read more