pwd command in Linux
The pwd
command in Linux, short for Print Working Directory, displays the absolute path of the current directory, helping users navigate the file system efficiently.
It is one of the first commands you use when you start learning Linux. And if you are absolutely new, take advantage of this free course:
pwd command syntax
Like other Linux commands, pwd also follows this syntax.
pwd [OPTIONS]
Here, you have [OPTIONS]
, which are used to modify the default behavior of the pwd command. If you don't use any options with the pwd command, it will show the physical path of the current working directory by default.
Unlike many other Linux commands, pwd does not come with many flags and has only two important flags:
Option | Description |
---|---|
-L |
Displays the logical current working directory, including symbolic links. |
-P |
Displays the physical current working directory, resolving symbolic links. |
--help |
Displays help information about the pwd command. |
--version |
Outputs version information of the pwd command. |
Now, let's take a look at the practical examples of the pwd command.
1. Display the current location
This is what the pwd command is famous for, giving you the name of the directory where you are located or from where you are running the command.
pwd
2. Display the logical path including symbolic links
If you want to display logical paths and symbolic links, all you have to do is execute the pwd
command with the -L
flag as shown here:
pwd -L
To showcase its usage, I will need to go through multiple steps so stay with me. First, go to the tmp
directory using the cd
command as shown here:
cd /tmp
Now, let's create a symbolic link which is pointing to the /var/log
directory:
ln -s /var/log log_link
Finally, change your directory to log_link
and use the pwd command with the -L
flag:
pwd -L
In the above steps, I went to the /tmp
directory and then created a symbolic link which points to a specific location (/var/log
) and then I used the pwd command and it successfully showed me the symbolic link.
3. Display physical path resolving symbolic links
The pwd command is one of the ways to resolve symbolic links. Meaning, you'll see the destination directory where soft link points to.
Use the -P
flag:
pwd -P
I am going to use the symbolic link which I had created in the 2nd example. Here's what I did:
- Navigate to
/tmp
. - Create a symbolic link (
log_link
) pointing to/var/log
. - Change into the symbolic link (
cd log_link
)
Once you perform all the steps, you can check the real path of the symbolic link:
pwd -P
4. Use pwd command in shell scripts
To get the current location in a bash shell script, you can store the value of the pwd command in a variable and later on print it as shown here:
current_dir=$(pwd)
echo "You are in $current_dir"
Now, if you execute this shell script in your home directory like I did, you will get similar output to mine:
Bonus: Know the previous working directory
This is not exactly the use of the pwd command but it is somewhat related and interesting. There is an environment variable in Linux called OLDPWD
which stores the previous working directory path.
This means you can get the previous working directory by printing the value of this environment variable:
echo "$OLDPWD"
Conclusion
This was a quick tutorial on how you can use the pwd command in Linux where I went through syntax, options, and some practical examples of it.
I hope you will find them helpful. If you have any queries or suggestions, leave us a comment.