Linux Directory Navigation: pwd, cd, mkdir, and Directory Stack

Navigating and structuring directories in Linux is fundamental to mastering the command-line environment. This guide covers core directory traversal, path resolution mechanics, directory creation, safe removal, and advanced stack-based navigation.


⚡ Quick Dive

Core Navigation Cheat Sheet

Command Action Example
pwd Print current working directory (logical path) pwd
pwd -P Print physical working directory (resolves symlinks) pwd -P
cd <dir> Navigate to target directory cd /var/log
cd or cd ~ Return to user's home directory cd ~
cd .. Move up to parent directory cd ..
cd - Switch to previous working directory ($OLDPWD) cd -
mkdir <dir> Create a new directory mkdir project
mkdir -p <path> Create nested directory tree (parents as needed) mkdir -p src/components/ui
rmdir <dir> Remove an empty directory safely rmdir empty_dir
pushd <dir> Push directory onto stack and switch to it pushd /etc/nginx
popd Pop top directory off stack and switch to it popd
dirs -v Display directory stack with index numbers dirs -v

Quick Start Examples

# Print physical path if inside a symlinked directory
pwd -P

# Create a project tree with one command using brace expansion
mkdir -p my-app/{src/{api,utils},tests,docs}

# Jump to a configuration directory, inspect it, and jump straight back
pushd /etc/nginx/conf.d
# ... perform work ...
popd

📖 Extended Guide

1. pwd — Print Working Directory

The pwd command outputs the absolute path of the directory you are currently located in.

Syntax

pwd [OPTIONS]

Key Options

  • -L (Logical): Displays the logical path, including symbolic links if traversed (default behavior).
  • -P (Physical): Resolves all symbolic links and outputs the true physical path on the filesystem.

Practical Example: Symlink Resolution

# Given /var/www/html is symlinked to /mnt/storage/web
$ cd /var/www/html

$ pwd -L
/var/www/html

$ pwd -P
/mnt/storage/web

2. Understanding Linux Paths

Paths dictate how the operating system locates a target in the directory tree:

  • Absolute Paths: Always begin with the root directory /. They are unambiguous regardless of where you currently are (e.g., /var/log/syslog).
  • Relative Paths: Calculated relative to your current working directory (e.g., ../config/app.json).

Path Shortcuts & Special Symbols

  • . (Current Directory): Represents the directory you are currently in (./script.sh).
  • .. (Parent Directory): Represents the immediate parent directory (cd ..).
  • ~ (Home Directory): Expands to the current user's home directory (e.g., /home/username or /root).
  • ~username: Expands to the specified user's home directory (e.g., cd ~john).
  • - (Previous Directory): Expands to the $OLDPWD environment variable.

3. cd — Change Directory

The cd command (a shell built-in) changes the current working directory for the active shell session.

Common Patterns

# 1. Move to home directory (equivalent shortcuts)
cd
cd ~

# 2. Move up two directory levels
cd ../..

# 3. Toggle between two directories quickly
cd /var/log/nginx
cd /etc/nginx
cd -   # Returns to /var/log/nginx
cd -   # Returns to /etc/nginx

# 4. Handle paths with spaces or special characters
cd "My Documents/Project Alpha"
cd My\ Documents/Project\ Alpha/

[!TIP] Use tab-completion (Tab key) aggressively when navigating paths to prevent typos and automatically escape spaces or special characters.


4. mkdir — Make Directories

The mkdir command creates one or more directories.

Syntax

mkdir [OPTIONS] <DIRECTORY_NAME...>

Key Options

  • -p, --parents: Creates intermediate parent directories if they do not exist. Prevents errors if the target directory already exists.
  • -m, --mode=MODE: Sets file permissions (octal) at creation time instead of defaulting to 777 - umask.
  • -v, --verbose: Prints a message for each created directory.

Advanced Examples

# Create nested directory structures safely
mkdir -p deployment/prod/configs

# Create a secure directory accessible only by the owner
mkdir -m 700 ~/.custom_keys

# Create structured skeletons with bash brace expansion
mkdir -p backend/{controllers,models,routes,services,config}

5. rmdir — Remove Empty Directories

The rmdir command safely deletes empty directories. Unlike rm -r, rmdir will fail if the directory contains any files or subdirectories, preventing accidental data loss.

# Remove an empty folder
rmdir old_reports

# Remove nested empty folders up the chain
rmdir -p parent/child/grandchild

[!NOTE] To remove a directory along with all its files and subdirectories, use rm -r <dir>. Always use caution with recursive deletion.


6. Directory Stack Navigation (pushd, popd, dirs)

When navigating deep or disparate directory trees in interactive sessions or complex shell scripts, the directory stack provides a LIFO (Last-In-First-Out) history.

Commands Overview

  • pushd <dir>: Saves the current directory onto the stack and changes to <dir>.
  • popd: Removes the top directory from the stack and navigates to it.
  • dirs -v: Lists the current directory stack with corresponding indices.

Workflow Example

# Start in home directory
$ pwd
/home/user

# Push /etc/nginx onto stack
$ pushd /etc/nginx
/etc/nginx /home/user

# Push /var/log onto stack
$ pushd /var/log
/var/log /etc/nginx /home/user

# View the stack
$ dirs -v
 0  /var/log
 1  /etc/nginx
 2  /home/user

# Pop the stack (returns to /etc/nginx)
$ popd
/etc/nginx /home/user

# Pop again (returns to /home/user)
$ popd
/home/user

7. Path Utilities: realpath and basename/dirname

For shell scripts and path validation:

# 1. Resolve full canonical absolute path
$ realpath ../../var/log/./nginx/
/var/log/nginx

# 2. Extract directory component
$ dirname /var/log/nginx/access.log
/var/log/nginx

# 3. Extract file/folder name component
$ basename /var/log/nginx/access.log
access.log