With the help of pwd command in Linux, you can instruct the system to print the working directory. Displaying the full system path that corresponds to the directory you’re currently working in, the pwd command is a promising utility.
The pwd command is a shell built-in, which means it is not an external executable but an integral section of the code that runs within a shell. It is executed to learn about the current working directory. The current Working Directory is nothing but the directory in which a user is currently operating.
No matter which code you intend to run, using the command prompt will make you operate within a directory, one way or another. By default, logging into the Linux system sets the home directory as the current working directory.
However, it is pretty easy to include quick alterations. To bring changes, all that you’ll need to do is launch the Terminal and use the cd command.
cd /tmp will change the directory to tmp
pwd Command in Linux
As already mentioned, the pwd command in Linux is a command-line utility that helps learn about the current working directory. The term pwd stands for Print Working Directory, which defines the purpose of this command.
Whenever you decide to run the pwd command in Linux, it will simply print the entire path of the directory you’re currently operating in. Just like
pwd is a built-in in modern shells.
pwd Command: Syntax
The general format of the pwd command in Linux looks something like this:
Using pwd Command in Linux
Now that you’re well exposed to the purpose and syntax of the pwd command in Linux, it is time to have a look at its uses. Whether the sudo privileges are accessible or not, you can still execute the pwd commands.
It is crucial to keep in mind the behavior of the pwd command is pretty much different from how the standalone /bin/pwd executable performs. Using the type command alongside the
-a flag will display all the locations that the pwd contains:
$ type -a pwd
pwd is a shell builtin pwd is /bin/pwd
Looking at the output, you can notice that the system prioritizes shell builtin over standalone
/bin/pwd. So if you want to employ the standalone pwd binary, you’ll need to type in the full path and assign it to the
Using the pwd Command in Linux to Find the Current Working Directory
The prime purpose of using the pwd command in Linux is to figure out the current directory a user is working in. Doing that is pretty straightforward.
Launch the terminal using the “Ctrl+Alt+T” key combination.
Run the following command:
You’ll see an output something like this:
This is nothing but the current working directory.
Interestingly, while using the pwd command in the PWD environment variable, it prints the same output, which is the working directory.
Run the following command:
$ echo $PWD
UNIX Shell Sessions with PWD
The pwd command in Linux is generally used with ls and cd commands for confirming the change or modifications in the current working directory. A quick look at the output below will help you understand better.
Arguments in the pwd Command
When we talk about the pwd command, it usually accepts two arguments, one is logical, and the other is physical.
-L (--logical): This argument instructs the system not to resolve symlinks and prints the symbolic path. It typically uses the $PWD command from the environment.
P (--physical): Employing this argument with the pwd command in Linux will display the physical working directory without any symbolic links.
The Logical Argument
The logical argument is similar to the pwd command when no id is specified. In other words, the pwd command when no id option is used behaves as if the
-L option is executed.
-L flag makes the usual pwd command use
$PWD from the environment, whether or not it contains the symlinks.
The Physical Argument
The working of
-P argument is different from how the logical one operates. To understand the situation better, let’s create a separate directory named “workingdirectory” alongside a symlink.
The newly created directory and symlink will be pointing to the directory as:
$ mkdir /tmp/workingdirectory $ ln -s /tmp/workingdirectory /tmp/symlink
Now when you’ll hover over to the
/tmp/symlink directory and execute the pwd command inside the terminal, the output will display
/tmp/symlink as the working directory.
However, using the
-P flag or the physical argument will print the directory to which the symlink you’re working on is pointed.
$ pwd -P
To put it in simple words, if you’re willing to avoid the symlinks, pass the
-P flag. This will make the pwd command display only the physical location.
Other Options of the pwd Command in Linux
To display the version of the pwd command, execute the following command:
$ /bin/pwd --version
Displaying pwd Information
Like most UNIX commands, the pwd command also features a help option. Run the following command to display the information about pwd.
$ /bin/pwd --help
Printing Environmental Variable
If you’re looking to print environmental variables, run the following command:
$ echo "$PWD $OLDPWD"
What if pwd Command is Not Available?
Although the pwd command line or variable is available in almost any shell we consider, in the rarest cases, you can
run CWD=$ (pwd)
With that, we’re done talking about the pwd command in Linux. Walking through this article, you’ll know how to invoke the command alongside varied arguments and display the current working directory.
If this guide helped you, please share it.