realpathx
Print the resolved absolute file name.
Mirrors GNU realpath. Run it as realpathx, or as gnux realpathx.
Synopsis
realpathx [OPTION]... FILE...GNU reference
Description
realpathx prints the canonical absolute name of each FILE: every symbolic link is expanded and every '.', '..' and repeated separator is removed. By default all components but the last must exist; -e requires the whole name to exist and -m requires none of it to. Links are resolved as they are met (-P, the default), so '..' after a link refers to the parent of the link's target; -L collapses '..' first, and -s does not expand links at all. --relative-to prints each name relative to a directory, and --relative-base does so only for names under that directory, leaving the rest absolute. A name that cannot be resolved is reported (unless -q) and the remaining names are still processed.
Permissions
Resolving a name needs search permission on every directory leading to it, including those reached through symbolic links. The files themselves are never opened, so no read permission on them is required.
Use cases
- Finding where a symlinked command or directory really lives.
- Normalizing user-supplied paths before comparing or storing them.
- Computing a path relative to a project root with
--relative-to. - Building the absolute name of a file that is about to be created.
- Checking that a path exists in a script with
-eand the exit status. - Cleaning up '.', '..' and doubled separators without touching symbolic links (
-s). - Choosing how '..' after a symbolic
linkis read: physically (-P) or logically (-L). - Emitting NUL-terminated names with
-zfor names that may contain newlines.
Flags
-e, --canonicalize-existing all components of the path must exist
-m, --canonicalize-missing no path components need exist or be a directory
--help display this help and exit
-L, --logical resolve '..' components before symlinks
--no-symlinks don't expand symlinks (same as --strip)
-P, --physical resolve symlinks as encountered (default)
-q, --quiet suppress most error messages
--relative-base DIR print absolute paths unless paths below DIR
--relative-to DIR print the resolved path relative to DIR
-s, --strip don't expand symlinks
--version output version information and exit
-z, --zero end each output line with NUL, not newlineExamples
Resolve a path to its canonical absolute name
realpathx ../lib/./util.goPrint the absolute name of the current directory
realpathx .Follow a symbolic link to the file it points to
realpathx bin/toolRequire the file to exist
realpathx -e /usr/bin/python3Test for existence quietly and branch on the exit status
realpathx -q -e config.yaml >/dev/null || echox 'config.yaml is missing'Normalize a path that does not exist yet
realpathx -m build/out/../report.txtClean up a path without expanding symbolic links
realpathx -s bin/toolSame thing, spelled out
realpathx --no-symlinks bin/toolRead '..' after a link logically (-L) rather than physically (-P, the default)
realpathx -L src/tools/.. && realpathx -P src/tools/..Print a path relative to another directory
realpathx --relative-to=/usr/lib /usr/bin/sortMake names under the project relative, but leave outside ones absolute
realpathx --relative-base=. lib/util.go /etc/hostsSeparate the output with NUL bytes instead of newlines
realpathx -z lib/util.go src/app/main.go | od -cFind the directory that really contains a file
dirnamex "$(realpathx scripts/run.sh)"Print several paths relative to a source directory with the gnux shell
gnux shell -c 'realpathx --relative-to=src lib/util.go bin/tool'Options
Each option has a page of its own, with its usage and examples.
- -e, --canonicalize-existing: All components of the path must exist.
- -m, --canonicalize-missing: No path components need exist or be a directory.
- -L, --logical: Resolve '..' components before symlinks.
- --no-symlinks: Don't
expandsymlinks (same as--strip). - -P, --physical: Resolve symlinks as encountered (default).
- -q, --quiet: Suppress most error messages.
- --relative-base: Print absolute paths unless paths below DIR.
- --relative-to: Print the resolved path relative to DIR.
- -s, --strip: Don't
expandsymlinks. - -z, --zero: End each output line with NUL, not newline.
Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake |
Help in the terminal
realpathx --help # options, first examples, exit status
gnux help realpathx # this page: the full manual
gnux help realpathx --OPTION # one option in depth, with its examples