Skip to content

realpath

Shows the full, absolute path of a file or directory, with all symbolic links and .. resolved.

realpath output

Syntax

realpath [OPTION]... FILE...

Options

Option Description
-e, --canonicalize-existing All parts of the path must exist, otherwise print an error
-m, --canonicalize-missing No part of the path needs to exist
-s, --no-symlinks Do not resolve symbolic links, only clean up . and ..
--relative-to=DIR Print the path relative to DIR
-q, --quiet Do not print error messages

Examples

Show the absolute path of a file:

realpath notes.txt
/home/nox/notes.txt

Find out where a symbolic link really points to:

realpath /bin/sh
/usr/bin/dash

/bin is a link to /usr/bin on all current distributions. On Debian and Ubuntu, sh is a link to dash, on RHEL and Arch to bash. realpath follows the whole chain.

Clean up a path with ..:

realpath ../../etc/ssh
/etc/ssh

Make sure that a path exists:

realpath -e missing.txt
realpath: missing.txt: No such file or directory

Without -e, realpath would print /home/nox/missing.txt, because by default only the directory has to exist.

Show a path relative to another directory:

realpath --relative-to=/etc /etc/ssh/sshd_config
ssh/sshd_config

When do I need it?

  • To find out which file a symbolic link really points to, for example in /etc/nginx/sites-enabled or /etc/alternatives
  • To get absolute paths for configuration files, cron jobs or systemd units
  • In scripts, to find the directory the script itself is located in

The script's own directory

This line gives a script the directory it is stored in, no matter from where it was started:

SCRIPT_DIR="$(dirname "$(realpath "$0")")"

readlink -f

In older scripts, you often see readlink -f instead of realpath. On Linux, both give the same result.

pwd ยท cd