Navigation and files

Command realpath

Show absolute path

Linux · Bash examples · commands run on the selected server

When to use realpath

Use realpath to normalise paths and resolve symbolic links before comparing or recording locations. Its behaviour for missing path components depends on the options and implementation, so distinguish inspection from existence checks.

Syntax

realpath [PARAMETERS] PATH…

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-e
Require the existence of all path components.
-m
Resolve missing path components.
--relative-to=DIRECTORY
Print the relative path from the given directory.

Usage examples

Get absolute path

realpath ./config/app.conf

The relative configuration path is converted into a resolved absolute path.

Check existing path

realpath -e ./README.md

The command succeeds only if every path component exists, making a missing file visible as an error.

Get relative path

realpath --relative-to=/srv /srv/app/config

The output expresses the configuration location relative to the specified server directory.

Notes and limitations

  • These examples use GNU realpath; available options depend on the implementation.
  • Resolving a symbolic link does not guarantee that the target will remain unchanged before you access it.

Common questions and troubleshooting

Does an absolute path prove a file exists?

Not with every default realpath mode. Use the existence-requiring option when you need that guarantee, and still handle races before opening the file.

Can these flags be used on every Mac or server?

These examples target GNU/Linux. BSD and other implementations may expose different flags; consult the installed manual before reusing them elsewhere.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man realpath.