The pwd Linux command prints the current path of the working directory, starting from the root (/). Use the pwd command to find your way through the structure maze of the Linux file system or to pass the working directory in a Bash script.
In this tutorial, you will learn how to use the pwd command.
- system running Linux
- Terminal window access (Ctrl+Alt+T)
- A text editor of your choice
pwd Syntax The pwd command takes the following
The available options are described in the next section
The pwd command
has two output states:
- 0. Success
- It is not zero. Failure.
Options provide additional functionality to the command. The
pwd command accepts the following options: -L, -logical pwd
. The following examples explain common use cases for the pwd command.
Example 1: Get
the working directory path
Running the pwd command without any options generates
the full path to the current working directory.
The command generates the absolute path of the current working directory. In this case, the path of the home directory.
However, if you followed a symbolic link to the current directory, running pwd prints the location of the symbolic link and not its destination.
In the following example, we create a symbolic link to the current directory. Following the link and executing the pwd command displays the symbolic link in the path: Example 2: Using the -P option The -P option instructs pwd to print the
physical working directory and avoids enumerating symbolic links. Therefore, the command prints the absolute path to the current working directory.
Although a symbolic link was used to change the directory, the pwd command prints only 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 symbolic links. In the following example, we use a symbolic link to navigate to a directory and run pwd -L: The output shows the path with the symbolic link.
Example 4: Print $PWD variable contents
The environment variable $PWD stores the path of the current directory. Print the contents
of the variable by executing: echo $PWD The command generates the
current working directory, including symbolic links, stored in the
Customize the output of
pwd Customize the output of the pwd command by storing its value in a variable and adding a message using the echo command.
The following example is a script where the pwd value is stored in the $p 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
: #!/bin/bash p=$(pwd) echo “You are currently in: $p”
3. Save the script and exit vi with the following command: :
4. Change the file permissions to make the script executable:
chmod +x directory.sh
the script: ./directory.sh The
script generates the current working directory with a custom message.
Example 6: Check the
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 version of pwd is installed by running: /bin/pwd -version
The command generates the binary version of pwd on the system
. Example 7
View previous working directory The $OLDPWD variable stores the path
of the previous working directory
. Check your previous location on the file system by running
: echo “$OLDPWD” The command
generates the previous working directory
Help File Use the -help
option to view the help file for the pwd command and the available options: pwd -help The result is a short help file with a list of available options. Example 9: Using pwd
In this example, we create a Bash Case statement that uses PWD to generate 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: vi
2. Enter the lines below
: #!/bin/bash echo “I need to see:”echo” 1 – My current physical directory path.” echo “2 – My current directory path, including symbolic links”. 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 symbolic links, is: $(pwd -L)”;; 3) echo “You were previously in: $OLDPWD”;; ESAC
3. Save and exit vi by running: :
4. Make the script executable with chmod: chmod
5. Run the script and choose an option
The script uses a case statement to provide three options and prints a corresponding output based on the response
. 6. Change the directory using a symbolic link,
run the script again and compare the outputs:
The output shows the physical location, although we use a symbolic link to change the directory
7. Also try the remaining options to see the result
Example 10: Set up
an alias for pwd
Create an alias that includes the -P option to avoid getting symbolic links 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 so that the system remembers the alias after the system restarts.
alias pwd=’pwd -P’; echo “alias pwd=’pwd -P'” >> ~/.bashrc
The output shows the physical location, although we follow a symbolic link to the current route
This tutorial showed how to use the pwd command on Linux to check your location quickly
If you’re interested in bash, check out our tutorial for bash features and how to use them. Alternatively, we recommend digging deeper into Linux commands in our list of all important Linux commands in one place.