Skip to main content

dirname — Extract Directory Path from a File Path

dirname is a simple but powerful utility that extracts the directory portion of a path.

It removes the last component (usually a filename) and returns only the parent directory.

This command is heavily used in:

  • Shell scripting
  • Backup automation
  • Deployment pipelines
  • WordPress VPS management
  • Cron jobs and log rotation

It performs string-based path parsing.
It does not check whether the file actually exists.

1. Core Purpose

If you have:


/var/www/html/index.php

dirname returns:


/var/www/html

It strips the final path component.

2. Installation

dirname is part of GNU coreutils and is installed by default on Linux.

Verify:

dirname --version

3. Core Syntax

dirname [OPTION] NAME...
  • NAME → File path (absolute or relative)
  • Multiple paths allowed
  • Output is printed one per line

4. Available Options

OptionPurpose
-
(none)Default behavior
-a, --multipleProcess multiple paths
-z, --zeroEnd output with null character
--helpShow help
--versionShow version

5. Fundamental Behavior Rules

Understanding how dirname behaves is important:

  1. It removes the final path component.
  2. If there is no slash, it returns ..
  3. If input is /, it returns /.
  4. It does not validate file existence.
  5. It handles trailing slashes automatically.

6. Practical Examples (WordPress VPS Context)

Basic Usage

dirname /var/www/html/index.php

Output:

/var/www/html

Use case: Identify WordPress root.

Plugin File

dirname /var/www/html/wp-content/plugins/hello.php

Output:

/var/www/html/wp-content/plugins

Use case: Detect plugin directory in scripts.

Step Up One Level

dirname /var/www/html/wp-content/

Output:

/var/www/html

Two Levels Up

dirname $(dirname /var/www/html/wp-content/plugins/hello.php)

Output:

/var/www/html/wp-content

Use case: Move from plugin → wp-content.

Root Directory

dirname /

Output:

/

Root remains root.

Relative Path

dirname ./index.php

Output:

.

Means current directory.

No Directory Component

dirname file.txt

Output:

.

If no slash exists, returns ..

Nonexistent File

dirname /not/exist/file.txt

Output:

/not/exist

Important: existence is not checked.

With Variable

F=/var/www/html/wp-config.php
dirname "$F"

Output:

/var/www/html

Always quote variables.

Multiple Inputs (-a)

dirname -a /etc/nginx/nginx.conf /var/log/nginx/error.log

Output:

/etc/nginx
/var/log/nginx

Batch parsing.

Null-Safe Output (-z)

dirname -z /var/www/html/wp-config.php

Output (null-terminated):

/var/www/html\0

Useful with:

xargs -0

Backup Example

file=/home/backups/db_2025.sql
echo "Backup directory: $(dirname "$file")"

Output:

Backup directory: /home/backups

Combine with basename

file=/var/www/html/wp-content/uploads/image.png
echo "$(dirname "$file")/$(basename "$file" .png).webp"

Output:

/var/www/html/wp-content/uploads/image.webp

Convert PNG → WebP while keeping directory.

dirname /var/www/current

Output:

/var/www

It does not resolve symlink targets.

Find All PHP Parent Directories

find /var/www/html -name "*.php" -exec dirname {} \; | sort -u

Sample output:

/var/www/html
/var/www/html/wp-admin
/var/www/html/wp-content/plugins

Use case: audit PHP directory distribution.

7. Script Automation Example

Backup compression example:

file=/home/backups/db_2025.sql
tar -czf "$(basename "$file" .sql).tar.gz" -C "$(dirname "$file")" .

This dynamically:

  • Extracts directory
  • Compresses from correct parent path

Very useful in cron jobs.

8. WordPress VPS Use Cases

  • Determine WordPress root from any core file
  • Detect plugin folder dynamically
  • Build dynamic backup paths
  • Automate log rotation
  • Parse deployment release paths
  • Work with symlink-based deployments

9. Common Mistakes

Not Quoting Variables

Wrong:

dirname $file

Correct:

dirname "$file"

Expecting Validation

dirname does not verify path existence.

Forgetting -a for Multiple Inputs

Without -a, behavior may be inconsistent across systems.

10. Best Practices

  • Always quote variables.
  • Combine with basename for full path splitting.
  • Use -z when piping into xargs -0.
  • Use command substitution for stepping up directories.
  • Remember: it is purely string-based.

11. dirname vs basename (Conceptual Pair)

TaskCommand
--
Get directorydirname
Get filenamebasename

Example:

file=/var/www/html/index.php
dirname "$file" # /var/www/html
basename "$file" # index.php

Together they fully split a path.

12. Cheat Sheet

dirname /path/file.txt
dirname file.txt
dirname -a file1 file2
dirname -z /path/file
dirname $(dirname /path/file)

13. Mini Quiz

  1. What does dirname /var/www/html/index.php return?
  2. What does dirname file.txt return?
  3. Does dirname check if the file exists?
  4. Which option allows multiple paths?
  5. How do you step two levels up from /var/www/html/wp-content/plugins/?

Summary

dirname is small but critical in automation.

It enables:

  • Clean path parsing
  • Dynamic directory detection
  • Reliable script logic
  • Safer backup and deployment workflows

In WordPress VPS operations, it becomes a foundational tool for path management and scripting precision.