How to Discover Your Directory With the pwd Command

Learn how to use the pwd command in Linux

You Are Here Marker on asphalt

Gaël Rognin / EyeEm / Getty Images

One of the most important commands you learn when using the Linux command-line interface is the pwd command, which stands for print working directory.

This guide shows you how to use the pwd command. It also shows you how to find the physical path to the directory you're working in and the logical directory you're working in.

How to Determine Which Linux Directory You're Currently In

To find out which directory you're currently in, open a command line, and run the following command:


The output for the pwd command will be something like this:


As you move around the system, the working directory changes to reflect your current position within the file system.

For example, if you use the cd command to navigate to the documents folder, the pwd command produces the following output:

/ho

What Does pwd Show When You Navigate to a Symbolically Linked Folder?

To answer this question, we set up the following scenario.

Imagine that you have the following folder structure:

  • home
    • gary
      • documents
        • folder1
        • folder2

Now imagine that you created a symbolic link to folder 2, as follows:

ln -s /home/gary/documents/folder1 /home/gary/d

The folder tree would now look like this:

  • home
    • gary
      • documents
        • folder1
        • folder2
        • accounts

The ls command shows the files and folders within a particular location:


If you ran the ls command against your documents folder, for accounts it would show something like this:

accou

Symbolic links point to another location within the file system.

Now imagine that you're in the documents folder and you use the cd command to move into the accounts folder. What would the output of pwd will be?

If you guessed that it would show /home/gary/documents/accounts, then you'd be correct. But, if you ran the ls command against the accounts folder, it shows you the files within the folder2 folder.

Look at the following command:


When you run the above command within a symbolically linked folder, you see the physical location, which in this case is /home/gary/documents/folder2.

To see the logical folder, you can use the following command:


This command would show the same folder as the pwd command on its own, which is /home/gary/documents/accounts.

Whether the command defaults to the physical path or the logical path depends on how you've set up and compiled pwd on your system. Therefore, best practice is to use the -P or -L switch (depending on which behavior you want to see).

How to Use the $PWD Variable

You can view the current working directory by displaying the value of the $PWD variable, as follows:


How to Display the Previous Working Directory

If you want to view the previous working directory, run the following command:


The output displays the directory you were in before you moved to the current directory.

Multiple Occurrences of pwd

The pwd command may behave differently based on how you set it up. A good example is in Kubuntu Linux.

The shell version of pwd, which you use when you run the pwd command, shows the logical working directory when you're within a symbolically linked folder. If you run the following command, however, you'll see that it shows the physical working directory when you're within a symbolically linked folder:


This output isn't helpful: You're essentially running the same command but getting the reverse result when you run it in a default mode. That's why it's good to get into the habit of using the -P and -L switches.

Helpful Switches With pwd

Two further switches are helpful with the pwd command. The first:


...displays the current version number for pwd.

When run against the shell version of pwd, the --version switch may not work. It will, however, work against the /bin/pwd version.

The other switch:


...displays the manual page to the terminal window.

Again, this switch doesn't work for the shell version of pwd, only against the /bin/pwd version.