Skip to main content

realpath — Resolve Absolute Canonical Paths

realpath converts any given path into its absolute canonical path.

It resolves:

  • Relative paths (. and ..)
  • Symbolic links
  • Redundant slashes
  • Nested references

The result is a fully expanded, unambiguous absolute path.

This makes it critical in production scripts, deployments, and WordPress VPS environments where symlinks (like current → releases/...) are common.

1. Core Purpose

Given:

./wp-content/../wp-config.php

realpath returns:

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

It removes ambiguity and normalizes the path.

2. Installation

realpath is part of GNU coreutils and is installed by default.

Verify:

realpath --version

3. Core Syntax

realpath [OPTION]... FILE...
  • Accepts one or multiple paths
  • Returns resolved absolute paths
  • Works purely at filesystem level (unlike dirname/basename which are string-based)

4. Available Options

OptionPurpose
---
-e, --canonicalize-existingRequire all path components to exist
-m, --canonicalize-missingAllow missing components
-s, --strip, --no-symlinksDo not resolve symlinks
-L, --logicalResolve logically (default)
-P, --physicalResolve physically
--relative-to=DIROutput path relative to DIR
--relative-base=DIRRelative if under base, else absolute
-q, --quietSuppress errors
--helpShow help
--versionShow version

5. Practical Examples (WordPress VPS Context)

Assume WordPress root:

/var/www/html

Resolve Relative Path

realpath ./index.php

Output:

/var/www/html/index.php

Use case: Confirm exact location before editing.

Resolve Directory

realpath wp-content/themes

Output:

/var/www/html/wp-content/themes

Useful in deployment scripts.

realpath current

Output:

/var/www/html/releases/release-2025-10

Confirms active release target.

Require Existing Path (-e)

realpath -e /etc/nginx/sites-enabled/wordpress.conf

Output:

/etc/nginx/sites-available/wordpress.conf

Fails if any component is missing.

Allow Missing Path (-m)

realpath -m /tmp/missing/file.txt

Output:

/tmp/missing/file.txt

Useful when preparing new file paths.

realpath -s current

Output:

/var/www/html/current

Keeps symlink name intact.

Normalize ..

realpath /var/www/html/../html/wp-config.php

Output:

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

Removes redundant parent references.

Relative Output

realpath --relative-to=/var/www/html /var/www/html/wp-content/uploads

Output:

wp-content/uploads

Useful for generating config-relative paths.

Relative Base

realpath --relative-base=/var/www/html /etc/nginx/nginx.conf

Output:

/etc/nginx/nginx.conf

Falls back to absolute because path is outside base.

Multiple Files

realpath index.php wp-config.php

Output:

/var/www/html/index.php
/var/www/html/wp-config.php

Resolve multiple entries at once.

Quiet Mode

realpath -q missing.file

Output:

(no output)

Prevents noisy errors in scripts.

Mounted Uploads Storage

realpath wp-content/uploads

Output:

/mnt/storage/wp-uploads

Confirms external storage mount.

Compare Logical vs Physical

realpath -L current
realpath -P current

Usually same output unless nested symlinks exist.

Logical: follows symlink as seen Physical: resolves physical disk layout

find -L /var/www/html -type l -exec realpath {} \;

Resolves all symlink targets.

Normalize Path in Script

FILE=../wp-config.php
ABS=$(realpath "$FILE")
echo "$ABS"

Ensures automation uses absolute path.

6. realpath vs readlink

Featurerealpathreadlink
--
Resolves relative pathsYesNo
Resolves symlinksYesYes (with -f)
Normalizes ..YesOnly with -f
Canonical absolute outputAlwaysOnly with -f
Allows missing pathsYes (-m)No

General rule:

  • Use realpath for canonical normalization.
  • Use readlink when you specifically need raw symlink targets.

7. WordPress VPS Use Cases

  • Resolve current symlink during deployments
  • Verify uploads mount path
  • Normalize paths in cron jobs
  • Ensure backup scripts use absolute paths
  • Confirm Nginx/Apache symlink targets
  • Audit storage mounts

8. Common Mistakes

Using -e on Missing File

realpath -e newfile.txt

Fails because file does not exist.

Use:

realpath -m newfile.txt

Expecting String-Only Behavior

Unlike dirname and basename, realpath interacts with filesystem.

Confusing -s

-s keeps symlink unresolved.

Remove it if you want actual target path.

9. Best Practices

  • Use realpath -e in production scripts for safety.
  • Use -m when preparing future file paths.
  • Prefer absolute paths in automation.
  • Combine with dirname and basename for full path manipulation.
  • Use --relative-to in configuration generation.

10. Cheat Sheet

realpath file.txt
realpath -e file.txt
realpath -m /tmp/new/file.txt
realpath -s symlink
realpath --relative-to=/var/www/html path
realpath --relative-base=/var/www/html path
realpath -q missing.file

11. Mini Quiz

  1. What does realpath guarantee in its output?
  2. Which option allows missing paths?
  3. What does -s change?
  4. How do you output a relative path?
  5. Why is realpath safer than using relative paths in automation?

Summary

realpath is essential for:

  • Canonical path resolution
  • Deployment safety
  • Symlink validation
  • Reliable automation scripts

In WordPress VPS environments with release symlinks, mounted uploads, and backup directories, realpath ensures every script operates on the exact intended location.