📍 Command: pwd (Print Working Directory)
The pwd command is a simple but essential utility. It answers the most basic question you’ll have while working in a terminal: "Where am I right now?"
1. The "Why"
In a graphical file manager, you can see your location in the address bar or the folder sidebar. In the terminal, while some custom prompts (like those in Arch Linux or Hyprland setups) show your current folder, they often truncate long paths to save space.
pwd provides the full, absolute path from the root (/) to your current location. This is crucial when:
- You are writing a script and need to know the exact path to a file.
- You are deep within a complex directory structure (like Android source code).
- You need to copy the current path to use in another command or configuration file.
2. Basic Syntax
pwd [OPTIONS]
Most of the time, you will just use it without any options:
pwd
3. Understanding the Output
When you run pwd, you get an Absolute Path.
- Absolute Path: Starts from the root
/(e.g.,/home/ahmed/projects/Preader). - Relative Path: Starts from where you are (e.g.,
../src/java).
4. Practical Examples
A. Checking Your Current Location
If your terminal prompt only shows ~ and you want to see the actual path:
pwd
# Output: /home/ahmed
B. Dealing with Symbolic Links (Shortcuts)
Sometimes, a folder is actually a "link" to another location on the disk. pwd has two flags to handle this:
- Logical Path (
-L): Shows the path including the shortcut (default behavior). - Physical Path (
-P): Shows the "real" physical location on the hard drive, bypassing any shortcuts.
# If /var/mail is a link to /var/spool/mail:
pwd -L # Output: /var/mail
pwd -P # Output: /var/spool/mail
5. Pro-Tips
- Copy to Clipboard: If you need to send your current path to a friend or paste it into a LaTeX document, you can pipe the output (on many Linux setups) like this:
pwd | xclip -selection clipboard - Variable Usage: In Bash scripting, you can store your current location in a variable to use it later:
CURRENT_DIR=$(pwd) - The
$PWDVariable: Bash actually keeps a built-in variable that always holds your current path. Typingecho $PWDdoes the same thing as running thepwdcommand.
6. Summary Reference
| Goal | Command |
|---|---|
| See current absolute path | pwd |
| See the "real" path (avoiding links) | pwd -P |
| See the path including links | pwd -L |