While working on Linux shells such as bash, ksh or zsh you may be required to know the directory in which you are working. In this tutorial, we will learn about the pwd command in Linux to check the current working directory.
Prerequisites
- A Linux computer.
- Some familiarity with the Linux command-line interface.
- Eagerness to learn new commands and try different command options.
pwd Command
The pwd command prints the current working directory on your Linux terminal. This command prints the absolute path of the current/present working directory. The pwd stands for print working directory.
Pwd is one of the simplest and most useful commands in Linux, AIX, HP-UX, and other UNIX-like operating systems. It is a shell built-in command, which means it does not call any external programs to run.
The current working directory is the Linux directory in which you are currently working. Any commands that you run in this directory, without specifying a directory path, will run only in this directory. When you log into your Linux system, the default current directory is your home directory, for example, /home/username. You can navigate to a different directory using the cd command, and then that directory becomes your current directory.
By default, pwd command ignores symbolic links and prints the directory path including the symbolic link.
Syntax
The basic syntax of the pwd command is:
pwd [options]
Pwd Command Examples
The pwd command is very easy to use and can be used in various ways. The following examples describe common use cases of this command.
Show Current Working Directory Path
Use the pwd command without any option to print the current working directory:
pwd
The above output shows the full path of the current working directory, which is the home directory in this case.
Print Symlinks in Present Directory
Use the pwd command with -L
option to print working directory that includes symbolic links in the path:
pwd -L
In this example, we are creating a symbolic links to the Pictures directory, then move to the symbolic link directory.
Print Physical Location of the Present Directory
Use the pwd command with -P
option to print the physical location of the current working directory, while avoiding the symlinks:
pwd -P
This output shows the symlinks directory when the pwd command is used without any option. But, when we specify the -P
option, it shows the physical directory instead of the symlink.
Print Present Directory Environment Variable
The environment variable of the current working directory is $PWD. Use the $PWD environment variable with the echo command to display the environment variable value:
echo $PWD
As you can see in this output, the $PWD
environment variable also includes symlinks by default. Similarly, you can use the $OLDPWD
environment variable to print the previous working directory:
echo $OLDPWD
Check Version of Pwd Command
Pwd is a shell built-in command and its binaries are stored in the /bin/pwd
directory. The actual binary version of this command might be different from the shell version. Use the following command to check the pwd binary version:
/bin/pwd --version
The pwd command, when run directly, runs the shell builtin pwd. To run the binary version, use this command from the /bin/pwd
directory.
Conclusion
In this tutorial, we learned about how to use the pwd command to print working directory. The pwd command is easy to learn and it comes handy when you feel lost while working in the directory structure.
Comments