pwd

file managementLinux/Unix
The pwd command is one of the most frequently used commands in Linux/Unix-like operating systems. pwd Print name of current/working directory

Quick Reference

Command Name:

pwd

Category:

file management

Platform:

Linux/Unix

Basic Usage:

pwd [options] [arguments]

Common Use Cases

    Syntax

    pwd [OPTION]...

    Options

    Option Description
    -L, --logical Use PWD from environment, even if it contains symlinks (default)
    -P, --physical Avoid all symlinks and report the physical directory
    --help Display help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the pwd command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    pwd
    Print the full path of the current working directory.
    # Advanced Examples Advanced
    pwd -L
    Print the logical current working directory (may contain symlinks).
    pwd -P Print the physical current working directory (with symlinks resolved). OLDPWD=$(pwd) cd /tmp echo "Changed from $OLDPWD to $(pwd)" Save the current directory before changing to a new one. cd "$(pwd)" Re-execute cd with the current directory (useful to resolve symlinks). echo "The current directory is: $(pwd)" Use pwd in a script to display the current directory. dirname "$(pwd)" Print the parent directory of the current directory. basename "$(pwd)" Print only the final component of the current directory.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `pwd` command, which stands for "print working directory," is one of the most fundamental commands in Unix-like operating systems. It displays the full pathname of the current working directory, helping users orient themselves within the file system hierarchy. In Unix-like systems, the file system is organized as a hierarchical tree structure, with the root directory (/) at the top. When navigating through this structure using commands like `cd`, it's easy to lose track of your current location. The `pwd` command solves this problem by showing exactly where you are in the file system. Key features and aspects of the `pwd` command include: 1. Simplicity: At its core, `pwd` does one thing and does it well - it tells you where you are in the file system. 2. Built-in Command: In most shells (like Bash), `pwd` is a built-in command, meaning it's implemented by the shell itself rather than being a separate executable. This makes it very efficient. 3. Logical vs. Physical Paths: Modern implementations of `pwd` offer two modes: - Logical mode (-L): Shows the path as it was navigated, potentially including symbolic links - Physical mode (-P): Resolves all symbolic links to show the actual physical path on disk 4. Environment Variable: The current working directory is typically stored in the PWD environment variable, which `pwd` often reads. You can access this directly with `echo $PWD` in most shells. 5. Essential for Scripts: In shell scripts, `pwd` is frequently used to determine the script's current location or to create absolute paths based on relative locations. Common uses of `pwd` include: - Orientation: Checking your current location in the file system when navigating directories - Script Path Resolution: Determining the absolute path of a script's directory for reliable file operations - Constructing Absolute Paths: Creating absolute paths from relative paths for commands that require them - Troubleshooting: Verifying that you're in the expected directory when commands produce unexpected results - Educational Purposes: Teaching new users about the file system hierarchy and navigation While simple, `pwd` is an essential tool in the Unix philosophy of having small, focused tools that do one job well. It works in conjunction with other navigation commands like `cd` (change directory), `ls` (list directory contents), and `find` (search for files) to provide complete control over file system navigation.

    Related Commands

    These commands are frequently used alongside pwd or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the pwd command works in different scenarios.

    $ pwd
    View All Commands