basename and dirname on Linux are heavily searched when writing backup loops, log rotators, and install scripts — they split a full path into directory and filename without brittle sed hacks.
Related: readlink,
test,
find.
basename for filename
Returns the last path component — includes extension.
bashbasename
basename /var/www/html/index.php
basename /data/archive.tar.gz

dirname for directory
Everything before the final slash.
bashdirname
dirname /etc/nginx/sites-available/app.conf

Strip suffix
basename -s .ext removes a known suffix for stem names.
bashstem
f='/logs/app.2026-03-01.log'; base=$(basename "$f"); stem=$(basename "$f" .log); echo "$base $stem"

Combine in scripts
Create output next to input or preserve relative layout.
TipQuote variables:
dir=$(dirname "$path") handles paths with spaces.
Quick Reference
- File:
basename /path/file - Dir:
dirname /path/file - Stem:
basename file .ext
Related tutorials
Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.