Resolve and canonicalize file paths, returning the absolute path.
The realpath command resolves symbolic links and relative paths (e.g., ., ..) in a given path and outputs the corresponding absolute path. It is commonly used in scripts to obtain the true location of a file or directory, avoiding path ambiguity caused by symbolic links or relative paths.
Unlike simply using pwd or string concatenation, realpath ensures that the output path is a unique, real, and accessible physical path.
realpath [options] file...
-e, --canonicalize-existing Only output if all components of the path exist.
-m, --canonicalize-missing Output the canonicalized path even if components do not exist.
-L, --logical Resolve symbolic links logically (default).
-P, --physical Resolve symbolic links physically.
-q, --quiet Quiet mode; do not output error messages.
-s, --strip-trailing-slashes Remove trailing slashes from the path.
--relative-to=DIR Output the path relative to DIR.
--relative-base=DIR If possible, output the path relative to DIR.
--help Display help information.
--version Display version information.
file The path of the file or directory to be resolved, which can be a relative path or a symbolic link.
realpath file.txt
realpath /usr/bin/python
realpath -m ./non/existent/path
realpath --relative-to=/usr /usr/bin/env