The pwd full form stands for "print working directory". It displays the full path of the current directory in which the user is located. The pwd comes useful when navigating the Linux filesystem, ensuring users know their current location, especially within deep directory structures or scripts, and before performing operations to confirm they're in the correct directory.
pwd is both a built-in command and an external command. This means that the shell itself provides a pwd command, but there's also a standalone /bin/pwd utility.
In many cases, just typing pwd without any options is sufficient.
Typing pwd in the terminal and pressing enter is all it takes.
The command returns an absolute path, showcasing the complete route from the root directory to your current location.
You can use the pwd command in conjunction with other commands. For instance, you might want to list the files of the current directory:
$ ls $(pwd)
The pwd command has a very simple syntax and is typically accompanied by a few options. The primary options available are: physical (
--physical) and logical (
--logical). This dictates how this location is presented, especially when symbolic links come into play.
Imagine you have a directory named /var/www/html and a symbolic link named web (under /home/ubuntu directory) that points to /var/www/html.
mkdir web ln -s /var/www/html web cd web/html
Now, when you use pwd -L (or simply pwd in many cases):
The output will likely be the path ending in /home/ubuntu/web/html because you navigated through the symbolic link.
In contrast, had you used pwd -P, the output would show the path ending in /var/www/html because this is the actual, physical location without considering the symbolic link's indirection.
pwd Environment Variables
When it comes to pwd and environment variables, there are a couple of direct interactions.
PWD environment variable holds the current working directory's path. This variable generally reflects the same value as the output of the pwd command.
This will print the current working directory.
The second one is the
OLDPWD environment variable contains the path of the directory you were in before the last directory change, typically via the cd command.
This will print the previous working directory. If you run
cd -, it will switch back to the directory stored in OLDPWD.