Where to start First steps track · 15 stages
Linux Labs

Paths — basename, dirname, realpath

cheat sheet · GNU coreutils https://lab.dimsanych.ru/en/wiki/paths/

Cheat sheet

  • basename /a/b/c.txt c.txt
  • basename /a/b/c.txt .txt c
  • dirname /a/b/c.txt /a/b
  • realpath ../x an 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.

Loading…