🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

realpath Command

Beginner File Management man(1)

Resolve absolute file path and symlinks

📅 Updated: Mar 16, 2026
SYNTAX
realpath [OPTIONS] FILE...

What Does realpath Do?

The realpath command resolves the absolute path of a file or directory, resolving all symbolic links, . (dot) and .. (dot-dot) references along the way. It outputs the canonical, fully-resolved path — the true location on disk.

realpath is essential in shell scripts where you need absolute paths regardless of the current working directory or symlink chains. It prevents issues with relative paths that break when scripts are called from different directories, and resolves symlinks that can mask the true location of files.

Common use cases include converting relative paths to absolute paths in scripts, resolving symlinks to find the real location of binaries or config files, generating canonical paths for comparison (two different paths may point to the same file), and building reliable file references in deployment scripts.

Options & Flags

OptionDescriptionExample
FILE Resolve the canonical absolute path realpath script.sh
-e All components must exist (error if not) realpath -e /path/to/file
-m No component needs to exist realpath -m /future/path/file.txt
-s Do not resolve symlinks realpath -s ./link
--relative-to=DIR Output path relative to DIR realpath --relative-to=/home /home/user/file
--relative-base=DIR Print relative if under DIR, absolute otherwise realpath --relative-base=/home /home/user/file

Practical Examples

#1 Get absolute path

Convert a relative path to an absolute path. Essential in scripts that may be called from any directory.
$ realpath ./script.sh
Output: /home/user/projects/myapp/script.sh

#2 Resolve symlinks

Follow symlinks to find the actual binary location.
$ realpath /usr/bin/python3
Output: /usr/bin/python3.11

#3 Script: get own directory

Find the real directory of the running script, even if invoked through a symlink.
$ SCRIPT_DIR=$(dirname "$(realpath "$0")") && echo "Script is at: $SCRIPT_DIR"

#4 Compute relative path

Calculate the relative path from one directory to another.
$ realpath --relative-to=/home/user /home/user/projects/app/src
Output: projects/app/src

#5 Verify path exists

Use -e to verify all path components exist. Returns error code 1 if file does not exist.
$ realpath -e /etc/nginx/nginx.conf && echo "File exists" || echo "File missing"

#6 Path for non-existent file

Resolve path even if it does not exist yet. Useful for generating paths before file creation.
$ realpath -m /var/www/newsite/index.html
Output: /var/www/newsite/index.html

Tips & Best Practices

Use in scripts for reliability: Always use realpath when your script needs to reference files relative to itself: BASEDIR=$(dirname "$(realpath "$0")"). This works regardless of how the script is invoked.
realpath vs readlink -f: realpath FILE is equivalent to readlink -f FILE on most systems. realpath has more options (--relative-to, -m) and is the recommended modern tool.
Compare file paths: Two different paths may point to the same file. Use realpath to canonicalize both and compare: [ "$(realpath a)" = "$(realpath b)" ] && echo "Same file"

Frequently Asked Questions

How do I get the absolute path of a file in Linux?
Use: realpath filename — it outputs the full absolute path. For scripts: FULL_PATH=$(realpath "$0")
What is the difference between realpath and readlink?
realpath resolves the full canonical path. readlink shows the target of a single symlink. readlink -f is equivalent to realpath. Use realpath for modern scripts.
How do I resolve symlinks in Linux?
realpath /path/to/symlink — follows all symlinks and outputs the final target. Use readlink (without -f) to see just the direct symlink target.
Does realpath work with non-existent files?
By default, the final component does not need to exist. Use -e to require all components exist, or -m to not require any component to exist.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →