How to Use the pwd Command on Linux

In this tutorial, we will show you how to use the pwd command on a Linux distribution.

The pwd command

The pwd command is short for print working directory and is used to output the full path of the current working directory. You can change the directory by using the cd command or list the contents of the current directory by using the ls command.

You can use this command to help locate where you currently are within a folder structure. It is also a very useful command to use within scripts where you might not know the exact folder structure of where the script is running.

This command is typically preinstalled as a shell builtin in most modern shells such as bash, zsh, and ksh. Primarily system administrators use the command as regular users do not normally need it.

There is not a considerable amount to cover with this basic command, but it is a handy command to know. So, if you are ready to learn, let’s get started by loading up the terminal on your machine.

Using the pwd Command

The PWD command by itself is incredibly simple and doesn’t require any extra flags or parameters. The standard syntax of the command is as follows.

pwd [option]…

To display the current working directory using the default behavior of pwd, simply enter the following line into the command prompt.

pwd

The output will be the path of the current working directory.

/home/dev

The pwd command accepts two different flags.

  • -L Will print the path even if it includes symlinks.
  • -P Will print the physical path, avoiding symlinks.

It is important to note that the behavior of pwd is slightly different from the /bin/pwd command.

Show Symbolic Links

A symbolic link is essentially a shortcut to another file or folder. Using symbolic links allows you to have multiple access points to the same set of files without having to create additional copies. You also only need to edit a file or folder in a single place.

1. For example, we will quickly make a symlink using the ln command. But, first, we must create a directory that our symlink will point to.

mkdir /example/normal

2. Now create a symbolic link from a new folder called symlink to our normal directory.

ln -s /example/normal /example/symlink

3. Change directory into the symlink folder.

cd /example/symlink

4. Enter the pwd command without any extra flags.

pwd

5. You will not see the symbolic link if you use the regular pwd command.

/example/symlink

6. If you wish to see a symbolic link (if it exists), you can simply use the -P option.

pwd -P

7. As you can see, the path is the one we set using the ln command.

/example/normal

Do Not Show Symbolic Links

You can use the -L option if you do not wish to see symbolic links and just want the actual path.

pwd -L

The output should show you the actual path and not the symbolic link.

/example/symlink

$PWD Environment Variable

You can also use the $PWD environment variable, which has the current working directory path stored in it. Using $PWD is the same as using the -L option on the pwd command.

 Simply echo the variable to get the value.

echo $PWD

The output should be the actual path as it is ignoring symbolic links.

/example/symlink

You can also get the path of the old or previous working directory by echoing the $OLDPWD environment variable.

echo $OLDPWD

The output should display the directory path you were previously in before you ran the cd command and changed directories.

/example

Conclusion

I hope by now, this guide has taught you how to use the PWD command effectively. It is a handy command for quickly printing the current working directory.

If you ever require more information regarding this command, you can always use the inbuilt manual pages tool. To do this, simply enter the following command.

man pwd

You can press q to quit the manual page.

We have plenty of other Linux tutorials if you want to learn more useful Linux commands. Also, if you have anything to add regarding this tutorial, be sure to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *