What is the pwd command?
In the Linux environment, knowing the exact location in the filesystem is essential for navigating, running scripts, and managing files. The pwd command, short for “print working directory”, is one of the simplest yet powerful tools offered by the terminal. In this article we will explore its operation, syntax, options, and some tricks that will make your daily work more efficient.
Basic syntax
The pwd command is invoked simply by typing:
pwd
This form shows the absolute path of the current directory, starting from the root of the filesystem (/). If you want to see the logical path, which respects symbolic links, you can use the -P option. By default, pwd shows the logical path (-L), but you can force the physical path with -P.
Usage examples
- Show the current directory:
$ pwd /home/user/documents - Force the physical path (ignoring symbolic links):
$ pwd -P /home/user/real_documents - Use pwd in a script to build paths:
#!/bin/bash BASE=$(pwd) cp file.txt "$BASE/backup/" - Combine pwd with other commands:
ls -la $(pwd)
Useful options
Although pwd is simple, it has some options that can be useful:
-L: shows the logical path (default value).-P: shows the physical path, resolving all symbolic links.--help: displays a brief help and exits.--version: displays the command version and exits.
In most distributions, the version of pwd comes from the GNU coreutils package, so its behavior is consistent across systems.
Tips and tricks
- Customize the prompt: You can include the output of pwd in your PS1 variable to always see the current directory:
export PS1='\u@\h:\w\$ 'Here \w already shows the directory, but if you prefer to use the command explicitly:
export PS1='\u@\h:$(pwd)\$ ' - Capture the output in a variable: In scripts, storing the result avoids repeated calls:
CURRENT_DIR=$(pwd) - Avoid surprises with symbolic links: If you work in directories that are links, remember to use -P when you need the real path.
- Compatibility: pwd works the same in bash, zsh, fish, and other shells, making it portable.
Conclusion
The pwd command, despite its apparent simplicity, is an essential piece in the arsenal of any Linux user. Knowing when to use the logical or physical path, how to integrate it into scripts, and how to customize your work environment will allow you to navigate with greater confidence and efficiency. The next time you ask yourself “Where am I?”, remember that pwd has the answer at the press of a key.
This post is also available in ESPAÑOL.