pwd Command in Linux Explained [With Examples]

Written by: Linuxopsys   |   Last updated: August 25, 2023

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.

Syntax

pwd [OPTIONS]

In many cases, just typing pwd without any options is sufficient.

Basic Usage

Typing pwd in the terminal and pressing enter is all it takes.

Example

pwd
displays the full path of the current directory

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)

Options

The pwd command has a very simple syntax and is typically accompanied by a few options. The primary options available are: physical (-P or --physical) and logical (-L or --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):

pwd -L

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 -P and pwd -L

pwd Environment Variables

When it comes to pwd and environment variables, there are a couple of direct interactions.

The PWD environment variable holds the current working directory's path. This variable generally reflects the same value as the output of the pwd command.

echo $PWD

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.

echo $OLDPWD

This will print the previous working directory. If you run cd -, it will switch back to the directory stored in OLDPWD.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment