Four small utilities for working with paths. basename and dirname cut the path string and don't touch the disk. realpath and readlink look at the file system: they resolve symbolic links, . and ... As a bonus — namei, which shows each step of a path with its permissions. The sandbox has GNU coreutils 9.4 and util-linux.
Syntax
basename path [suffix]
basename -a [-s suffix] path…
dirname path…
realpath [options] path…
readlink [options] link…
namei [options] path…
basename — the last part of a path
| Call | Result |
|---|
basename /var/log/nginx/access.log | access.log |
basename /var/log/nginx/access.log .log | access — suffix removed |
basename /var/log/ | log — the trailing slash is ignored |
basename / | / |
basename .log .log | .log — the suffix isn't removed if it equals the whole name |
| Option | What it does |
|---|
-s suffix | remove a suffix; implies -a |
-a | accept several paths, one result per line |
-z | separate results with a NUL byte |
dirname — everything but the last part
| Call | Result |
|---|
dirname /var/log/nginx/access.log | /var/log/nginx |
dirname file.txt | . — the current directory |
dirname /var/log/ | /var |
dirname / | / |
dirname a/b c/d | a and c — one line per path |
realpath — canonical absolute path
Resolves all links, ., .. and extra /. By default all components except the last must exist.
| Option | What it does |
|---|
| (no options) | physical path: links resolved, the last component may not exist |
-e | all components must exist, otherwise an error |
-m | nothing has to exist — pure string normalization |
-s | don't resolve links, only remove ., .., // |
-L | process .. first, then links (like cd/pwd in bash) |
-P | links first, then .. (the default) |
--relative-to=dir | print the path relative to a directory |
--relative-base=dir | relative path only if the target is inside the directory; otherwise absolute |
-q | don't print errors |
-z | separate results with a NUL byte |
readlink — contents of a symbolic link
| Option | What it does |
|---|
| (no options) | the path stored in the link, as is (may be relative). Not a link — empty output and code 1 |
-f | canonical path: resolve all links recursively; the last component may not exist |
-e | like -f, but everything must exist |
-m | like -f, but nothing has to exist |
-n | don't print the trailing newline |
-v | report errors (by default readlink is silent) |
readlink -f and realpath are almost the same; realpath is clearer and can do relative paths.
namei — walk a path step by step
Shows each path component, its type and where links point. The main use is to find out which directory along the path lacks permissions.
| Option | What it does |
|---|
-l | long format: permissions + owner + group (-m -o -v) |
-m | permissions of each component |
-o | owner and group |
-x | mark mount points with D instead of d |
-n | don't follow symbolic links |
$ namei -l /etc/os-release
f: /etc/os-release
drwxr-xr-x root root /
drwxr-xr-x root root etc
lrwxrwxrwx root root os-release -> ../usr/lib/os-release
drwxr-xr-x root root ..
drwxr-xr-x root root usr
drwxr-xr-x root root lib
-rw-r--r-- root root os-release
Examples
# file name without the extension
basename /data/report.csv .csv
# remove the suffix from several paths
basename -s .log /var/log/app.log /var/log/db.log
# the directory the script itself lives in (works when called from anywhere)
script_dir=$(dirname "$(realpath "$0")")
# where a link points: as stored and the final path
readlink /srv/app/current
readlink -f /srv/app/current
# path from one directory to another — for ln -s
realpath --relative-to=/etc /var/log
# normalize a path that doesn't exist yet
realpath -m /srv/app/../data//new
# why a user can't read a file: permissions at each step
namei -l /srv/app/config/secret.yml
# the same without external commands — with bash itself
f=/srv/app/data.tar.gz
echo "${f##*/}" # data.tar.gz — like basename
echo "${f%/*}" # /srv/app — like dirname
Pitfalls
basename/dirname know nothing about the file system. dirname link returns the link's directory, not the target's. For the real location — realpath.
readlink without -f returns the path as stored — often relative (../current/x) and counted from the link's directory, not the current one.
readlink on a regular file silently prints nothing and returns 1. With -v you see the reason: "Invalid argument".
realpath and nonexistent directories. realpath /no/such is an error because /no doesn't exist. If you only need string normalization — -m.
.. and links. If current is a link to releases/v2, then realpath current/.. gives /srv/app/releases (physically), while realpath -L current/.. and cd current/.. give /srv/app.
- The
basename suffix is a string, not a pattern: basename a.tar.gz .gz → a.tar, while '*.gz' removes nothing.
- Spaces. Always quote both the argument and the substitution:
dirname "$(realpath "$0")".
See also
ln · ls · stat · chmod · variables · find