Linux pwd Command


The pwd command in Ubuntu (and other Linux-based systems) stands for Print Working Directory. It displays the full, absolute path of the current directory you’re in, showing you exactly where you are in the file system. This command is helpful for keeping track of your location as you navigate directories.

Syntax

pwd

1. Basic Usage of pwd

Simply type pwd and press Enter to get the full path of the current working directory.

pwd

Example output:

/home/user/Documents

In this example:

  • /home/user/Documents is the full path to the current directory, starting from the root directory /.
  • Each directory in the path is separated by a /.

2. When pwd is Useful

  • Confirmation: When navigating through directories with cd, pwd helps you confirm your current location.
  • Script Execution: In scripts or when working with files, pwd helps ensure you're working in the correct directory.
  • Absolute Path Information: Since it displays the full path, pwd is useful for copying or referencing paths elsewhere.

3. Using pwd with Options

In Ubuntu, pwd has an option for handling symbolic links.

  • pwd -P: Shows the physical path by resolving symbolic links. By default, pwd may show the logical path, which might include symbolic links. Using -P bypasses symbolic links and displays the true directory path.

Example:

pwd -P

If you're in a directory that was accessed through a symbolic link, pwd -P will show the actual location instead of the link.

Example Scenario

Suppose you’re in the /home/user/Documents directory and create a symbolic link to /home/user/Documents/Projects called LinkToProjects. If you navigate to this link using cd LinkToProjects:

  • pwd might show /home/user/Documents/LinkToProjects (logical path).
  • pwd -P would show /home/user/Documents/Projects (physical path).

Summary

The pwd command is a simple but essential command in Ubuntu that displays the current working directory. It’s useful for tracking your location within the file system, especially when running commands or scripts that depend on the exact directory path. Using pwd -P helps to get the actual path, bypassing symbolic links if needed.