pwd

pwd

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.

Basic Usage

The syntax for the pwd command is straightforward:

pwd

Examples

Displaying the Current Directory

Simply typing pwd will display the absolute path of the current working directory:

$ pwd
/home/user

This output shows that the current directory is /home/user.

Options

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.

Using the -L Option

The -L option (logical) is the default behavior and shows the logical path, which includes symbolic links:

$ pwd -L
/home/user

Using the -P Option

The -P option (physical) shows the actual physical path by resolving any symbolic links:

$ cd /home/user/symlink_to_dir
$ pwd -P
/home/user/actual_dir

If /home/user/symlink_to_dir is a symbolic link to /home/user/actual_dir, using pwd -P will display the resolved physical path.

Practical Use Cases

Scripting

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"

Navigation

When navigating deep directory structures, pwd helps you confirm your current location:

cd /var/log/apache2
pwd
# Outputs: /var/log/apache2

Checking Symlinks

When 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/target

Conclusion

The 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.

help

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
    

Last updated