realpath
Resolve symbolic links to absolute path
TLDR
Display the absolute path for a file or directory
Require all path components to exist
Resolve ".." components before symlinks
Disable symlink expansion
Suppress error messages
SYNOPSIS
realpath [OPTION]... FILE...
PARAMETERS
-e, --canonicalize-existing
All components of the path must exist. This is the default behavior.
-m, --canonicalize-missing
Path may not exist. Do not require that components exist.
-L, --logical
Resolve symlinks logically. All but the last component of the path are resolved logically (like pwd -L).
-P, --physical
Resolve symlinks physically. All components of the path are resolved physically (like pwd -P). This is the default behavior.
-q, --quiet
Suppress most error messages.
-s, --strip
Don't resolve the last path component if it's a symbolic link.
-z, --zero
End each output line with NUL, not newline. Useful for processing with xargs -0.
--relative-to=DIR
Output paths relative to DIR. If FILE is not under DIR, the absolute path is returned.
--relative-base=DIR
Compute path relative to base directory DIR. Similar to --relative-to but with different behavior if FILE is not under DIR.
--version
Display version information and exit.
--help
Display help and exit.
DESCRIPTION
realpath is a Linux command-line utility used to return the canonicalized absolute pathname of a file or directory. This means it resolves all symbolic links, processes any . (current directory) and .. (parent directory) components, and returns the absolute path from the root directory.
It's particularly useful in scripts and automation where you need to ensure you're referencing the actual location of a file, rather than a symlink or a path containing relative components. For example, if you have a symlink ~/mylink pointing to /usr/local/bin/myapp, realpath ~/mylink would output /usr/local/bin/myapp. By default, realpath requires that all components of the path exist, ensuring the returned path refers to an existing entity. It's crucial for robustness in file system operations and for simplifying paths to their true, unambiguous form.
CAVEATS
By default, realpath requires all path components to exist to return a canonical path. If any part of the path (except the final component when using --canonicalize-missing) does not exist, an error will be reported.
When using --relative-to, if the target path is not located within the specified base directory, realpath will return the absolute path instead of a relative one.
ERROR HANDLING
realpath exits with a non-zero status if any specified path cannot be resolved or does not exist (unless -m is used and the non-existence is only for the final component). This makes it suitable for use in shell scripts where robust error checking is important.
SECURITY IMPLICATIONS
While primarily a utility for path resolution, its function of revealing the true path by resolving all symlinks actually helps mitigate certain types of path traversal vulnerabilities by showing the actual target location, rather than a manipulated symbolic link.
HISTORY
The realpath command is part of the GNU Core Utilities (coreutils) package, which provides essential tools for file, text, and shell manipulation on Unix-like operating systems. It was developed to provide a robust and standardized way to resolve paths, leveraging underlying system calls like realpath(3) (the C library function). Its widespread inclusion in coreutils ensures its availability and consistent behavior across most Linux distributions, making it a fundamental utility for scripting and system administration tasks.