The Linux ‘pwd’ command stands for print working directory. Traversing the Linux file system requires understanding your current working directory to navigate different locations easily. Luckily, the pwd command lets you know your current location, and using it is straightforward.
Read on as this post discusses the Linux pwd command and gives examples of using it in different scenarios.
Understanding the “pwd” Command and Its Syntax
In Linux, the “current working” directory represents the location where you perform different tasks. By default, opening the Linux terminal will show your home directory as your current working directory.
If you change the directory using the Linux cd command, the new directory becomes your current working directory. The pwd command follows the simple syntax below.
$ pwd [OPTIONS]
The pwd command is a simple yet valuable command, and it only takes two options. Therefore, mastering how to utilize it becomes very straightforward.
Examples of How to Use the “pwd” Linux Command
The pwd command can be used with the -L or -P options. The two allow you to specify if you want symbolic links resolved when printing the current working directory.
The -L or –logical flag resolves symbolic links, if any, and displays the logical current working directory.
The -P or —physical doesn’t resolve symbolic links and only shows the physical current working directory. It is the default option selected when you run the pwd command with no flag.
Example 1: Basic Usage of the “pwd” Command
Running the pwd command with no flag will use the -P option and show the current physical working directory. The below example gives the absolute path confirming that we are in the /Desktop location of our home directory.
$ pwd
Example 2: Working with Options
We already mentioned that the pwd command can take two options/flags. To demonstrate how it handles a symbolic link depending on the selected flag, let’s quickly use the ln command to create a symbolic link.
We’ve created a symbolic link for the /usr/bin/ path and named it ‘ls_demo.’
$ ln -s /usr/bin/ ls_demo
We’ve also used the ls command to verify that our symbolic link was created.
Next, to understand their difference, let’s run the pwd command using the two options.
Scenario 1: Using the -L Flag
The -L option is added to resolve the symbolic link. Run it as shown below.
$ pwd -L
Let’s change our current directory to the created symbolic link using the cd command. Notice how our output shows the absolute path of our current working directory after resolving the symbolic link.
Read Also: How to change the current working directory using the cd command.
Scenario 2: Using the -P Flag
Unlike the -L option, when we use the -P, which is the default behavior, we get the actual path of our current directory without resolving the symbolic link we created.
$ pwd -P
Example 3: Using the Binary pwd Command
You can also use the binary pwd, expressed as /usr/bin/pwd, which works like using the pwd -P. The example below shows how to get the same output running the binary pwd and the pwd -P.
$ /usr/bin/pwd
Example 4: Using the $pwd Environmental Variable
When working with environmental variables, such as when creating scripts, you can store the current working directory as an environmental variable.
Let’s have an example where we create an environmental variable “current_dir.” We must then assign its value to the $(pwd) results, which will fetch the current working directory.
The environmental variable will display the current working directory, as shown below.
$ current_dir=$(pwd)
$ echo $current_dir
Conclusion
The pwd command in Linux helps with printing the current working directory. You can use it with two flags depending on your goal. This post has discussed the pwd command and given different examples of using it when navigating the Linux file system.