15 ‘pwd’ (Print Working Directory) Command Examples in Linux

Here are 15 practical examples of the pwd command (Print Working Directory) in Linux, including basic and advanced use cases.

Alby Andersen

The pwd command in Linux stands for “print working directory”. It is used to display the full path of the current directory you are in. This command is particularly useful when navigating the filesystem, as it helps you confirm your location before performing operations like creating, deleting, or moving files. The pwd command is simple but essential for scripting, debugging, and ensuring you’re in the correct directory.

It supports two modes: logical (default, which respects symbolic links) and physical (resolves symbolic links to their actual paths). Whether you’re working in the terminal or writing scripts, pwd is a go-to tool for verifying your current directory.

Basic Usage

pwd

Prints the current working directory path (e.g., /home/user/documents).


Store Output in a Variable

current_dir=$(pwd)
echo "Current directory: $current_dir"

Use pwd in scripts to store the directory path in a variable.


pwd -P

Displays the “physical” path, resolving symbolic links (e.g., /home/user/real_dir instead of /home/user/link_dir).


Show Logical Path (Default)

pwd -L

Shows the “logical” path (default behavior), preserving symbolic links.


Use in Shell Scripts

#!/bin/bash
echo "Script launched from: $(pwd)"

Embed pwd in scripts to log the execution directory.


Redirect Output to a File

pwd > current_directory.txt

Save the current directory path to a file.


Combine with cd for Navigation

old_dir=$(pwd)
cd /new/directory
# Do something...
cd "$old_dir"

Save the current directory, navigate elsewhere, then return.


Use in Terminal Prompts

PS1='\u@\h:$(pwd)\$ '

Add the current directory to your terminal prompt (modify ~/.bashrc).


Check Directory in a Pipeline

pwd | xargs echo "Current path:"

Pipe the output to another command (e.g., xargs, grep).


Verify Working Directory in Cron Jobs

* * * * * /path/to/script.sh > /tmp/cron.log 2>&1 && echo "Ran from: $(pwd)" >> /tmp/cron.log

Debug cron jobs by logging the execution directory (note: cron uses its own $PWD).


Compare with $PWD Environment Variable

echo "pwd: $(pwd) | \$PWD: $PWD"

Compare the command output with the $PWD variable (usually identical).


Use in Conditional Statements

if [[ $(pwd) == "/home/user" ]]; then
  echo "You're in the home directory!"
fi

Check if the current directory matches a specific path.


Find Absolute Path of a Script

#!/bin/bash
script_dir=$(pwd)
echo "Script location: $script_dir"

Use pwd to determine where a script was executed from.


Resolve Relative Paths

absolute_path="$(pwd)/file.txt"

Convert a relative path to an absolute path (e.g., ./file.txt/home/user/file.txt).


Check for Built-in vs. External Command

type -a pwd

Identify if pwd is a shell built-in or external binary (most shells have it as built-in).


Bonus: Alias for Frequent Use

alias whereami='pwd'
whereami  # Same as pwd

Create a shortcut for pwd if you use it often.


Key Notes

  • pwd has no arguments – it simply prints the directory.
  • Use -P to resolve symbolic links (e.g., in /var/www/html when inside /www symlink).
  • Always quote "$(pwd)" in scripts to handle paths with spaces.

This command is deceptively simple but essential for scripting and navigation! 🐧

Share This Article