pwd
Print working directory
TLDR
Print the current directory
Print the current directory, and resolve all symlinks (i.e. show the "physical" path)
Display help
SYNOPSIS
pwd [OPTION]
PARAMETERS
-L
Prints the logical path. This is the default behavior. It shows the path as if symbolic links were part of the directory structure, rather than resolving them to their physical targets.
-P
Prints the physical path. This resolves all symbolic links in the current working directory to their true underlying path, effectively showing the canonical, absolute path.
DESCRIPTION
The pwd command (Print Working Directory) is a fundamental utility in Unix-like operating systems, including Linux. It displays the full pathname of the current working directory.
When you navigate the file system using commands like cd, pwd helps you confirm your exact location within the directory hierarchy. This is particularly useful when dealing with complex directory structures, symbolic links, or when you've lost track of your position.
By default, pwd usually prints the logical path, meaning it resolves symbolic links to the path you used to get there, rather than the underlying physical path. For instance, if you cd into a symbolic link, pwd will show the path including the symlink name, not the path to the actual target directory. This behavior can be altered using options.
CAVEATS
In most modern shells (like Bash, Zsh), pwd is often a shell built-in command. This means it's an integral part of the shell itself, rather than a separate executable program.
While this generally improves performance, it also means that the behavior (especially regarding -L and -P) can sometimes vary slightly between different shell implementations of the built-in and the standalone utility (if one exists). The primary caveat is understanding the difference between logical and physical paths when navigating through symbolic links, as misinterpreting the output can lead to unexpected behavior in scripts or manual operations.
SHELL BUILT-IN VS. EXECUTABLE
While pwd exists as an executable (e.g., /bin/pwd), it is almost always implemented as a shell built-in in popular shells like Bash, Zsh, and Ksh. This means that when you type pwd, the shell executes its own internal version of the command, which is faster as it avoids the overhead of launching an external process. You can verify this using type pwd or which pwd (which might point to the external executable but not tell you it's a built-in). The behavior of the built-in can sometimes slightly differ from the standalone utility, though for pwd, the core functionality is identical.
ENVIRONMENT VARIABLE PWD
Many shells maintain an environment variable named PWD that stores the current working directory. The pwd command often just outputs the value of this variable. For example, you can see it with echo $PWD. This variable is automatically updated by the shell whenever you change directories using the cd command. This is another reason why pwd is fast, as it typically just reads an already available shell variable.
HISTORY
The pwd command has been a staple in Unix-like operating systems since their early days, reflecting the fundamental need for users to know their current location within the hierarchical file system. Its simplicity and utility have ensured its continued presence across various Unix distributions and Linux.
Historically, its behavior regarding symbolic links has been refined, leading to the introduction of options like -L and -P to explicitly control whether the logical or physical path is displayed. It's often implemented as a shell built-in for efficiency, making it one of the most frequently used commands in interactive terminal sessions.