Pwd Command in Linux (Current Working Directory)

Introduction

Before we begin talking about the Current Working Directory, let's briefly understand - What is the Current Working Directory?

The user's current working directory is the location where he or she is now working. You're operating in a directory every time you interact with your command prompt.

Your current working directory is set to your home directory by default when you connect to your Linux system. The cd command is used to change the working directory.

In this tutorial, you will learn to use pwd command in Linux. We will also address a few FAQs on pwd Command in Linux (Current Working Directory).

To change the current working directory to /tmp, for example, type:

cd /tmp

The path to the current working directory may be displayed in the shell prompt if you have customized bash or zsh prompt.

pwd Command

The pwd command is used to print the current working directory. It's one of Linux's most fundamental and commonly used commands. When the command is run, it prints the full path to the current working directory.

Most current shells, such as bash and zsh, provide pwd as a built-in shell. It's behavior differs slightly from that of the standalone /bin/pwd program. To see all locations containing pwd, use the following command:

type -a pwd
Output

pwd is a shell builtin
pwd is /bin/pwd

The shell built-in takes precedence over the standalone executable, as shown in the output below, and it is utilized whenever you type pwd. Type the complete path to the file /bin/pwd if you wish to use the standalone pwd binary.

How to Check your Current Working Directory

In your terminal, type pwd to find out what directory you're in:

pwd

You will get an output like below:

Output

/home/vega

The PWD environment variable is printed by the pwd command.

If you type, you'll get the same result:

echo $PWD
Output

/home/vega

Only two arguments are accepted by the pwd command:

  • -L (--logical) - Symlinks are not resolved.
  • -P (—physical) - Show the physical directory without symbolic links.

    If no option is supplied, pwd behaves as if the -L option is specified.

Let's construct a directory and a symlink linking to it to better understand how the -P option works:

mkdir /tmp/directory
ln -s /tmp/directory /tmp/symlink

If you go to the /tmp/symlink directory and type pwd in your terminal, you'll see:

pwd

Your current working directory is /tmp/symlink, according to the output:

Output

/tmp/symlink

If you execute the same command with the -P option, you'll get the following results:

pwd -P

The directory to which the symlink points will be printed by the command:

Output

/tmp/directory

FAQs on pwd Command in Linux (Current Working Directory)

How do I use the pwd command in Linux? 

Using the pwd command is straightforward. Open a terminal and type pwd, then press Enter. The output will display the absolute path of the current working directory.

What is the difference between absolute and relative paths shown by the pwd command? 

The pwd command always displays the absolute path, which represents the full path from the root directory to the current working directory. In contrast, relative paths are expressed relative to the current directory and do not start from the root.

Can the pwd command show the symbolic link path instead of the actual path? 

No, by default, the pwd command shows the actual (canonical) path of the current working directory, not the path of a symbolic link pointing to it if one exists. To display the symbolic link path, you can use the -P or --physical option, like pwd -P.

Can the pwd command display the username as part of the output? 

No, the pwd command only displays the absolute path of the current working directory, not the username or any other additional information. To display the username, you can use the whoami command.

Is there any option or flag available for the pwd command? 

No, the pwd command does not have any additional options or flags. It is a simple command that only displays the absolute path of the current working directory.

Can the pwd command display paths with environment variables? 

No, the pwd command returns the actual directory path without expanding environment variables. If you want to view a path with expanded environment variables, such as $HOME or $USER, you can use echo or other shell commands.

Can the pwd command be run by any user? 

Yes, any user can use the pwd command to discover their current working directory. It does not require special permissions or elevated privileges.

Conclusion

The current working directory is the location from which you run terminal commands.

To see the current working directory, use the pwd command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.