Linux Command Reference ยท Files and Directories
Linux realpath command
Reference for the Linux realpath command: print absolute resolved paths, handle symlinks and missing files, and compute relative paths with --relative-to.
realpath turns any path into an absolute, canonical one: it makes the path absolute, removes . and .., collapses repeated slashes and resolves every symbolic link. It is the tool to reach for when you need an unambiguous name for a file, for comparing two paths, or when a script receives relative paths from users.
It can also express one path relative to another with --relative-to, which is useful for building portable symlinks and for readable output.
realpath [OPTION]... FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
realpath [-eLmPsqz] [--relative-to=DIR] [--relative-base=DIR] FILE...
| Option | Description |
|---|---|
-e, --canonicalize-existing |
All path components must exist. |
-m, --canonicalize-missing |
No component needs to exist. |
-s, --strip, --no-symlinks |
Do not expand symlinks (still removes . and .. textually). |
-L, --logical |
Resolve .. before symlinks. |
-P, --physical |
Resolve symlinks as encountered (default). |
--relative-to=DIR |
Print the result relative to DIR. |
--relative-base=DIR |
Print relative paths only if they lie under DIR; otherwise absolute. |
-q, --quiet |
Suppress most error messages. |
-z, --zero |
End lines with NUL. |
Returns: 0 if every argument was resolved; non-zero if any failed. With no -e or -m, every component except the last must exist.
Usage notes
- Default strictness: by default all but the last component must exist. Use
-mwhen you want a path for something you are about to create. ..and symlinks: physically,link/..is the parent of the link's target. Logically (-L) it is the directory containing the link. The default treats it physically, as the kernel does.realpathversusreadlink -f: they agree for ordinary cases.realpathhas the better name and the--relative-tofeature;readlink -fis older and is present on slightly more systems.realpathis a GNU coreutils tool (since 8.15). It is absent from very old systems, BusyBox may have a reduced version, and macOS only gained one recently.- Empty argument:
realpath ""fails; it does not mean the current directory. Userealpath .. - Safe argument handling: quote variables, and use
--before names that may begin with a dash:realpath -- "$path". - Not a security check by itself: resolving a path and then using it is racy (symlinks can change between the check and the use). For untrusted paths, resolve and verify the prefix, then open without following links where you can.
- Relative symlink creation:
ln -s "$(realpath --relative-to="$(dirname "$link")" "$target")" "$link"builds a link that survives moving the whole tree (or just useln -sr).
Related
readlink,dirname,basename,pwd,ln- Lessons: Navigating the filesystem, Hard and symbolic links