The pwd command in Unix and Linux stands for "print working directory." It is used to display the full path of the current directory you are in. This command is essential for navigation and scripting, as it helps you know your exact location within the file system hierarchy.
The syntax for the pwd command is straightforward:
pwdSimply typing pwd will display the absolute path of the current working directory:
$ pwd
/home/userThis output shows that the current directory is /home/user.
The pwd command has a few options that can modify its behavior:
-L(Logical): Prints the logical current working directory, resolving symbolic links.-P(Physical): Prints the physical current working directory, without resolving symbolic links.
The -L option (logical) is the default behavior and shows the logical path, which includes symbolic links:
$ pwd -L
/home/userThe -P option (physical) shows the actual physical path by resolving any symbolic links:
$ cd /home/user/symlink_to_dir
$ pwd -P
/home/user/actual_dirIf /home/user/symlink_to_dir is a symbolic link to /home/user/actual_dir, using pwd -P will display the resolved physical path.
In shell scripts, pwd is often used to get the current directory's path, which can then be used for various purposes, such as logging or setting paths dynamically:
#!/bin/bash
current_dir=$(pwd)
echo "The script is running in: $current_dir"When navigating deep directory structures, pwd helps you confirm your current location:
cd /var/log/apache2
pwd
# Outputs: /var/log/apache2When working with symbolic links, pwd -P can be useful to understand the actual physical directory you are in:
cd /path/to/symlink
pwd -P
# Outputs: /real/path/to/targetThe pwd command is a fundamental tool in Unix and Linux environments, essential for understanding and managing your current location within the file system. Its simplicity and effectiveness make it a staple in everyday command-line usage and scripting. Whether you are navigating directories, writing scripts, or working with symbolic links, pwd provides the necessary functionality to keep track of your working directory.
pwd: pwd [-LP]
Print the name of the current working directory.
Options:
-L print the value of $PWD if it names the current working
directory
-P print the physical directory, without any symbolic links