How to show current working directory's path on Linux
Note: Before delving into this Answer, an overview of Linux may be helpful.
Knowing our current location within a file system is crucial for managing files inside Linux. One of the most basic command for this purpose is pwd, which stands for “print working directory.” In this Answer, we’ll explore how to use the pwd command to display the path of the current working directory on a Linux system.
When we open a terminal in a Linux system, we start in a specific directory known as the user’s home directory. As we navigate through the file system using commands like cd (change directory), our current working directory changes accordingly. Enter ls in the terminal above to see the content of the current directory.
For this example, cd into any directory. An example of one such command is:
cd usercode
Now, we’ll use the pwd command:
pwd
This will output the full path of the directory we’re currently in, i.e., /usercode.
Flags
Let’s explore some flags we can append to this command.
The -logical flag
We can use -L or –logical as a flag:
pwd -L
This option above displays the logical current working directory. It does not resolve symbolic links, showing the path exactly as we navigate to it. For example, if we are in usercode directory, the above command will display /usercode as the output.
The -physical flag
We can use -P or –physical as a flag:
pwd -P
This option displays the physical current working directory. It resolves symbolic links, showing the “real” path.
For this example, we’ve created an example directory. Navigate to it as follows:
cd ~/logical_physical_demo/main_directory/symlink_to_subdirectory
Test both of these commands in the terminal provided prior.
Implementation
Use the terminal below to enter commands:
Knowledge test
Now we have understood how to display the current working directory path in Linux, let's test our knowledge on this:
What does the pwd command stand for in a Linux system?
Present working directory
Print working directory
Path working directory
Position working directory
Summary
Summary of -P and -L Flags
Flag | Description | Example |
| Displays the physical current working directory by resolving the symbolic links. |
|
| Displays the logical current working directory, without resolving the symbolic links. |
|
Unlock your potential: Linux series, all in one place!
To continue your exploration of Linux, check out our series of Answers below:
How to terminate a process running on particular port in Linux?
Learn how to terminate a process on a specific port in Linux usingkill -9 $(lsof -i:<port> -t)to free up resources.How to create Symlink in Linux?
Learn how a symlink in Linux creates a shortcut to a file or folder, aiding navigation and manipulation.What are soft and hard links in Unix/Linux?
Learn how soft links reference file paths as shortcuts, while hard links point directly to file inodes, ensuring data retention even after deletion.How to show current working directory's path on linux
Learn how to use thepwdcommand in Linux to display the current directory path, with-Lfor logical paths and-Pfor physical paths.
Free Resources