Paths — basename, dirname, realpath
cheat sheet · GNU coreutils https://lab.dimsanych.ru/en/wiki/paths/Cheat sheet
-
basename /a/b/c.txtc.txt -
basename /a/b/c.txt .txtc -
dirname /a/b/c.txt/a/b -
realpath ../xan absolute path -
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"a script's own directory
Options
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.