Each of the three utilities has its own small set of options. basename -s
strips a suffix without a separate sed call, -a takes several paths at
once. realpath -e requires the whole path to actually exist, while -m
does the opposite — it computes the path as a plain string even if the file
doesn't exist yet; confusing the two is a common source of bugs in scripts
that prepare a path for a file they're about to create themselves.
| Option |
What it does |
Example |
-m --canonicalize-missing |
realpath: compute the path even if it, or part of it, does not exist. |
realpath -m /nope/x |
-s --suffix SUFFIX |
basename: strip the given suffix from the name. |
basename -s .txt /a/b/c.txt |
-e --canonicalize-existing |
realpath: require the whole path to exist; otherwise fail. |
realpath -e /etc/hosts |
-s --strip |
realpath: do not resolve symlinks, only . and ... |
realpath -s ../x |
-a --multiple |
basename: multiple names at once, each on its own line. |
basename -a /a/x /b/y |
-z --zero |
Separate output entries with a NUL byte instead of a newline. |
basename -z /a/b/c.txt |
Nothing found. Try a shorter query.