The Beginner’s Guide to the pwd Command in Linux

pwd-command-linux-featured

In this article we are going to take a look at the pwd command, one of the most basic commands in Linux.

We will find out how to use the pwd command to write to standard output the full path name of your current directory and will learn the difference between the built-in pwd command and the external pwd command written by Jim Meyering.

Also read: Mastering the “Kill” Command in Linux

What Does PWD Do??

pwd is an acronym for “Print Working Directory.” The pwd command is one of the most frequently used commands by Linux system administrators. It works on Linux and other UNIX like operating systems and is a built-in shell command, available on Bash shell, korn, ksh, and so on.

The pwd command prints the current working directory name with the complete path starting from root (/). It is also an environmental variable.

Difference Between Shell “pwd” and “/bin/pwd”

Linux shells usually come with two types of pwd command: built-in pwd and binary version. To find out, you can use the following command:

type -a pwd

The screenshot below shows that bash shell comes with both the built-in pwd command and binary version.

types-of-pwd

What’s a Current Working Directory?

The current working directory is the directory in which you are currently operating commands such as ls, cat, or cd using bash or korn shell or zsh (zshell).

Also read: The Beginner’s Guide to pstree Command on Linux

Syntax of PWD Command

Every Linux command has its own syntax. Without basic knowledge of command syntax, executing commands on a Linux terminal might seem like a tedious task.

Below is a basic syntax of the pwd command:

pwd [options]

Pwd command comes with different options which allows us to use this command in different ways. However, you can still use the pwd command without any arguments.

Let’s take a look at the most basic options of the pwd command. To find a list of pwd command options, you can type pwd --help on the terminal.

list-pwd-options

  • -L: Prints the value of $PWD if it names the current working directory. In other words, it prints or displays the current working directory.

current-working-directory

Even without the -L, just typing the pwd on terminal does the same thing.

current-directory

  • -P: Prints the physical directory without any symbolic links. A symbolic link is a special kind of file that creates a shortcut to a file or points to another file. Print the physical directory without any symbolic links by using the commands below:
pwd -P

physical-directory

Assuming you have used the cd command several times and want to know your current working directory, you can use the following commands to do so:

echo $PWD

print-current-directory

pwd

print-working-directory

Both commands display your current working directory.

To find the version of the built-in pwd command on your terminal, you can use the binary version to do so:

/bin/pwd  --version

pwd-version

Conclusion

The pwd command is one of the most commonly used commands on Linux computers. It is quite simple to use when you know what it does.

Image credit: Russian hacker hacking the server in the dark by Lifestyle discover/Shutterstock

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Michael Aboagye

Michael wears many hat in the opensource industry. He is based in Accra, Ghana. He revels in anything Linux and Devops.