Master the Linux ‘pwd’ Command: A Comprehensive Guide

Peter Hou
3 min readApr 13, 2023

--

This article provides a comprehensive guide to the Linux ‘pwd’ command, including its history, use cases, and various aspects. The ‘pwd’ command is used to display the absolute path of the current working directory and is particularly helpful for users who need to keep track of their location in the file system or when running scripts that require the full path of a directory. The guide also covers commonly used parameters, most common use cases, tricky skills, and important notes to keep in mind when using the command.

Instructions

This article provides a detailed guide for beginners on the Linux ‘pwd’ command. We will explore the history, use cases, and various aspects of the ‘pwd’ command, along with detailed examples and advanced tips.

History of this command

The ‘pwd’ command, short for ‘print working directory,’ has been a fundamental part of Unix-based systems, including Linux, since their inception in the 1970s. It allows users to determine their current location within the file system.

When and why to use it

The ‘pwd’ command is used to display the absolute path of the current working directory. It is particularly helpful for users who need to keep track of their location in the file system or when running scripts that require the full path of a directory.

How to use it

Using the ‘pwd’ command is simple. Type ‘pwd’ in the terminal and press Enter:

$ pwd

The command will display the absolute path of the current working directory.

The commonly used parameters

The ‘pwd’ command has two main options:

-L or --logical: Displays the logical current working directory, taking symbolic links into account.

$ pwd -L

-P or --physical: Displays the physical current working directory, ignoring symbolic links.

$ pwd -P

Most common use cases

  1. Determining your current location in the file system.
  2. Providing the full path of the current working directory for scripts or other commands.

The tricky skills

Combining ‘pwd’ with other commands: You can use the output of the ‘pwd’ command as input for other commands by using command substitution. For example, to list the contents of the current directory, you can use:

$ ls "$(pwd)"

Creating a custom prompt: You can customize your terminal prompt to include the current working directory using the ‘PS1’ environment variable:

$ export PS1='\\w $ '

What needs to be noted

  1. If your working directory contains symbolic links, using the P option will display the actual physical path, while the L option will display the logical path, including symbolic links.
  2. Although ‘pwd’ is a built-in shell command, there is also a standalone ‘pwd’ executable located in /bin/pwd. Generally, the built-in version is used, but in some cases, the standalone executable might be called instead.

Conclusion

The ‘pwd’ command is a simple but essential tool for navigating the Linux file system. With this comprehensive guide, you now have a solid understanding of its history, usage, parameters, and advanced tips. Mastering the ‘pwd’ command will help you keep track of your location within the file system and improve your overall Linux experience.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.