Skip to content

pwd Command: Print Working Directory

Summary

The pwd command is a simple yet essential utility for displaying the absolute path of the current working directory. It's your compass in the Linux filesystem.

Introduction

The pwd command stands for "print working directory". It's a fundamental command in Linux and other Unix-like operating systems used to determine the directory you are currently operating in. Think of it as asking your computer, "Where am I?". It outputs the full path, starting from the root directory (/), to your current location. This is particularly useful when navigating through complex directory structures or when writing scripts that rely on knowing the exact location of files and directories.

Use Case and Examples

The primary use case of pwd is simply to display your current directory. However, its utility extends to scripting and more complex scenarios.

Basic Usage

pwd
This is the most basic usage. Running pwd will simply print the absolute path of your current working directory to the terminal. For example, the output might be /home/user/documents.

Using pwd in a script to create a directory

#!/bin/bash

# Get the current working directory
current_dir=$(pwd)

# Create a new directory inside the current working directory
mkdir "$current_dir/new_directory"

echo "Created directory: $current_dir/new_directory"
This script uses pwd to dynamically determine the current working directory and then creates a new directory named new_directory inside it. This ensures the script works correctly regardless of where it's executed.

Combining pwd with other commands to locate a file

find $(pwd) -name "my_file.txt"
Here, pwd provides the starting directory for the find command. This searches for a file named "my_file.txt" within your current working directory and all its subdirectories.

Commonly used flags

Flag Description Example
-L or --logical Displays the logical current directory, which may contain symbolic links. This is usually the default behavior. pwd -L
-P or --physical Displays the physical current directory, resolving any symbolic links. This will show the actual directory, not the link. pwd -P
--help Displays help information about the pwd command. pwd --help
--version Displays version information about the pwd command. pwd --version


Share on Share on

Comments