Cantech Knowledge Base

Your Go-To Hosting Resource

How to Display the Current Working Directory in Linux Using the pwd Command?

You must know what is your current location when you are working with Linux. The operating system has a structure where everything is stored inside directories. So, when you are working, you will move between directories. Therefore, you might sometimes forget where you are. That is when the pwd command helps you find out your exact location in the system and makes navigation much easier.

This guide explains its basic usage, different options, and practical examples. It also explains how to use it in different scenarios, such as automation, scripting, and error handling.

What is pwd Command?

The command pwd stands for ‘print working directory’. As mentioned, pwd command is a utility used to show the full path of the current working directory.

You will usually start in a default folder when you open the terminal. Pwd displays where you are in the system’s folder structure with the output showing the full path starting from the root directory.

It is therefore very useful when working on a complex file structure or when you simply need to verify where you are within the system.

For example, if you are inside a folder called Documents, and you use pwd, it will show /home/user/Documents. This way, you can be sure about your current location in the system.

Also, it is often used in scripts to track the directory the script is being executed from; to ensure the paths are correct.

In short, pwd is a simple command but it can save you a lot of time. It is one of the basic commands that every beginner should know.

Basic Syntax of the pwd Command

The syntax of the pwd command is quite simple. You run the below command and it will display the full absolute path of your current directory.

$ pwd [options]

So, pwd command also comes with a couple of options that can change the way it displays the directory path. They are described below –

  • -L

This is the default option. It displays the logical path of the directory showing where you are in the directory structure along with any symbolic links.

  • -P

This displays the physical path and does not include symbolic links to their actual physical location in the filesystem.

To explain more –

Logical Path (-L) example – if you have a symbolic link pointing to another directory, the logical path will include the link. It shows as it appears in your terminal environment.

Physical Path (-P) shows the real directory. It does not include symbolic links and shows the exact physical location of files in the system. So, this option will display where the symbolic link is pointing to in the filesystem.

Basic Examples of Using pwd

Below are a few simple examples of using the pwd command:

Show the Current Working Directory

You can type pwd and press enter. This will display the path to the directory you are currently in.
Example:

$ pwd
/home/user/documents

Show the Logical Path

You can get the logical path (that also includes symbolic links) using the -L option.
Example:

$ pwd -L
/home/user/links/myfolder

Show the Physical Path

You can get the physical path using the -P option. This will give you the exact location on the filesystem without any symbolic links.

Example:

 $ pwd -P3

/home/user/actual_folder

Practical Uses of the pwd Command

We have now covered the basics, so we will discuss the practical use cases for the pwd command. You will see how it can be used with other commands to create powerful scripts and make directory navigation more efficient.

Using pwd in Shell Scripts

It is common to use pwd to capture the current directory in scripting (especially with Bash). It is useful when you want to create paths relative to your script’s location or you want to ensure your script runs from the right directory.
Let’s discuss an example –

=> Create a simple script with my_script.sh command in a text editor
=> Add the below simple Bash script that uses pwd to print the directory where the script is being run from.

#!/bin/bash
current_dir=$(pwd)
echo "The script is running in: $current_dir"

This script assigns the output of pwd to a variable called current_dir; then it prints the directory path.

=> Give this script an executable permissions

$ sudo chmod +x my_script.sh

Then, run it –

$ ./my_script.sh

Output: The script is running in: /home/user/scripts

Using pwd with cd

Another great way to use pwd is with the cd command. Combining them and change your current directory and instantly verify your new location.
For example, you can change your working directory and then display it using pwd in a single line –

$ cd /home/user/documents && pwd

This command will change to the /home/user/documents directory and then display that directory’s full path –

/home/user/documents

Using pwd with ls

You can use pwd with ls to not only see where you are but also view the contents of that directory.
Example:

$ pwd && ls

This will first display the current directory and then list all the files and subdirectories within it.

Storing the Working Directory in a Variable

You can store the current working directory in a variable for later use with the below command –

$ current_dir=$(pwd) && echo "You are currently in: $current_dir"

This command stores the path in the current_dir variable and then prints it.

Using pwd with find

You can use pwd with the find command to search for files in your current directory.
For example:

$ find $(pwd) -name "*.log"

This command will search for all .log files in the current directory and its subdirectories.

Using pwd for Error Handling

Say, you are working with scripts and you need to check if a directory exists before performing actions. The pwd command can help in error handling by showing you the exact directory path in case something goes wrong.
Here’s an example of error handling:

$ if [ ! -d "/some/directory" ]; then
     echo "Directory not found, current location is: $(pwd)"
   fi

This will print the working directory and show you where the script is running from in case the directory /some/directory doesn’t exist.

Automating Tasks Using pwd

This is one of the best features of the pwd command. It can automate the tasks that depend on the current working directory. You can write scripts that check the directory and then perform actions based on that information.
For example – You have a script that should only run from a specific directory so you can check if the current directory matches the expected one:

=> Create a sample script file with nano pwd_script.sh and add the below to the file to check the directory before running

 #!/bin/bash
if [ $(pwd) != "/expected/directory" ]; then
    echo "Please navigate to /expected/directory before running this script."
    exit 1
fi

This script will check if you are in the correct directory before proceeding. If not, it will print a message and stop the execution. However, first, make the script executable to run it.

$ chmod +x pwd_script.sh

Now, execute it –

$ ./pwd_script.sh

=> If you’re not in the expected directory, it will tell you where you need to be.

Conclusion

The pwd command is very simple but it is an incredibly powerful tool if you want to manage directories and navigate the filesystem in Linux.

It adds convenience to various tasks such as shell scripting, error handling, automation, and when you need to confirm your current directory.

Further, options like -L and -P can give you more control over how the directory path is displayed. Also, it is especially helpful in complex environments with symbolic links.

It is easy to use pwd in different scenarios – run a quick command or write advanced scripts. Get more familiar with it and you will start to see how it fits into your workflow while making directory management smoother and more efficient.

April 22, 2025