← Full reference

basename, dirname, realpath, readlink

Taking paths apart — file name, directory, absolute path, where a link points

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

CallResult
basename /var/log/nginx/access.logaccess.log
basename /var/log/nginx/access.log .logaccess — 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
OptionWhat it does
-s suffixremove a suffix; implies -a
-aaccept several paths, one result per line
-zseparate results with a NUL byte

dirname — everything but the last part

CallResult
dirname /var/log/nginx/access.log/var/log/nginx
dirname file.txt. — the current directory
dirname /var/log//var
dirname //
dirname a/b c/da and c — one line per path

realpath — canonical absolute path

Resolves all links, ., .. and extra /. By default all components except the last must exist.

OptionWhat it does
(no options)physical path: links resolved, the last component may not exist
-eall components must exist, otherwise an error
-mnothing has to exist — pure string normalization
-sdon't resolve links, only remove ., .., //
-Lprocess .. first, then links (like cd/pwd in bash)
-Plinks first, then .. (the default)
--relative-to=dirprint the path relative to a directory
--relative-base=dirrelative path only if the target is inside the directory; otherwise absolute
-qdon't print errors
-zseparate results with a NUL byte

readlink — contents of a symbolic link

OptionWhat it does
(no options)the path stored in the link, as is (may be relative). Not a link — empty output and code 1
-fcanonical path: resolve all links recursively; the last component may not exist
-elike -f, but everything must exist
-mlike -f, but nothing has to exist
-ndon't print the trailing newline
-vreport 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.

OptionWhat it does
-llong format: permissions + owner + group (-m -o -v)
-mpermissions of each component
-oowner and group
-xmark mount points with D instead of d
-ndon'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