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/basenamewhich are string-based)
4. Available Options
| Option | Purpose |
|---|---|
| -- | - |
-e, --canonicalize-existing | Require all path components to exist |
-m, --canonicalize-missing | Allow missing components |
-s, --strip, --no-symlinks | Do not resolve symlinks |
-L, --logical | Resolve logically (default) |
-P, --physical | Resolve physically |
--relative-to=DIR | Output path relative to DIR |
--relative-base=DIR | Relative if under base, else absolute |
-q, --quiet | Suppress errors |
--help | Show help |
--version | Show 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.
Resolve Deployment Symlink
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.
Do Not Resolve Symlink (-s)
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
Audit Symlinks
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
| Feature | realpath | readlink |
|---|---|---|
| - | - | |
| Resolves relative paths | Yes | No |
| Resolves symlinks | Yes | Yes (with -f) |
Normalizes .. | Yes | Only with -f |
| Canonical absolute output | Always | Only with -f |
| Allows missing paths | Yes (-m) | No |
General rule:
- Use
realpathfor canonical normalization. - Use
readlinkwhen you specifically need raw symlink targets.
7. WordPress VPS Use Cases
- Resolve
currentsymlink 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 -ein production scripts for safety. - Use
-mwhen preparing future file paths. - Prefer absolute paths in automation.
- Combine with
dirnameandbasenamefor full path manipulation. - Use
--relative-toin 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
- What does
realpathguarantee in its output? - Which option allows missing paths?
- What does
-schange? - How do you output a relative path?
- Why is
realpathsafer 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.