
What is the PWD command in Linux?
There are various situations when you may need to know the exact path of your present working directory.
The PWD command in Linux is useful in many cases and it will show you the full path to your current working directory or “present working directory” which is what PWD stands for.
Whether you’re working on an rsync command, making sure you’re in the proper location before removing a file, or wanting an absolute path for a recursive removal of files, the PWD command is a helpful command line tool.
Within this article we’ll explain how the PWD command can be used and some examples of handy ways to utilize it on the command line interface in Linux.
Using the PWD command in Linux
The PWD command is quite simple and you only need to enter it in using the following syntax within your terminal on the command line interface:
$ pwd
Doing so will print your current directory path (current location) of your user:

The above gives you the absolute path to your users location such as we see above where the current path of our user is /var/www/html.
We can also switch users, use a quick cd command to move to our home directory and use PWD to show our users home directory location within the file system:

Flags/Options for the PWD command in Linux
There are a couple of flags (options) which can be passed to the PWD command such as -P or -L.
- When using -P it will print your users symbolic path
- When using -L it will print your users actual path
Using -P with the PWD command
As example lets say we have a symbolic link created from a location of /home/endofdayz/pub to /home/endofdayz/html.
When our user is within /home/endofdayz/pub (which is our symbolic link location) if we were to use the -P flag along side the PWD command we would then see the actual path as our present working directory:

From the above we see that -P will show us the actual path for where the symbolic link points us but ignore symbolic links when printing out the path.
Using -L with the PWD command
On the other hand if we were to use the -L flag along side the PWD command we would then see the symbolic link path as our present working directory:

Using the PWD command when setting variables for a specific task
Running some commands in a screen or a tmux session where you need to set variables such as a directory path?
In this example I’ll show how you can use the PWD command when setting variables such as below where we will spin up a secondary MySQL/MariaDB instance for data recovery.
1. Open up a screen session
We’ll first start up a screen session on the Linux command line and name it “restore”:
$ screen -S restore

2. Setting variables for our commands using PWD
So first lets change our directory to where the MySQL/MariaDB restore data is located, then we’ll set our “dir” (directory) variable:

3. Using the PWD command printed output as our variable:
Now we’ll just copy the PWD command output and use it to set our “dir” variable which will be needed for this entire procedure:

Above we did the following:
- We used PWD to get the absolute path to our current directory.
- Copied the PWD output and used it to set our “dir” variable.
- Confirmed the variable was set properly with the “echo $dir” command.
4. Proceed with remaining steps in procedure
Now we’ll update some file permissions and file ownerships using the variable we set after using PWD command to get the location for our restore directory:

5. Finishing the procedure
Now we can access the second MySQL/MariaDB instance using this command to show database:
$ mysql -h localhost -S $dir/socket.mysql -e 'show databases;'
And we can create a MySQL Dump file of a database from that backup using the following syntax (just fill in the database name desired):
$ mysqldump -h localhost -S $dir/socket.mysql $database > $database.sql
After that we can shut down the second MySQL/MariaDB instance since its no longer needed:
$ mysqladmin -h localhost -S $dir/socket.mysql shutdown
From the above we see that using PWD at times can save time and make things easier.
Rather than needing to type out the entire path for a variable we can instead use PWD, copy the output, then fill it in for the variable we were setting.
Using the PWD command to know your location when removing files
Using the PWD command when recursively deleting files
In general it is always best practice to use absolute paths if you’re removing files recursively.
For this example we’ll show how you can use PWD to remove a directory after obtaining its absolute path on the Linux command line.

Here we’re within the directory we want to remove as well as anything inside of it (recursively).
We then used the PWD command to obtain that full path and then used it within our recursive removal command.
Using the PWD command to confirm your location if removing individual files
On the other hand, sometimes if you’re just removing individual files you don’t really need to specify an absolute path but want to confirm you’re in the correct location before completing a removal.
Here is an example of using PWD so the command prints your current directory to confirm it is correct location before proceeding with file removal:

Overall in this example we’re just wanting to remove a php.info file for security reasons, but before we do we want to confirm the directory we are working within.
This is useful for note taking as well which Linux Administrators will do while working inside client servers.
By doing so, providing that entire output shown above shows you removed the file within that specific location.
Conclusion
It is quite often you may need to print the current directory of where your user is located within on the Linux command line.
The PWD command is helpful for various reasons such as when you’re needing to record notes and want to show the present working directory of a user location before a file removal, setting variables for a present working directory and so on.
This command line tool will print working directories for where your user is currently located and is one of the basic commands any Linux user should learn to utilize on the command line.
Leave a Reply