Skip to content
Linux Basics: Split Path Strings with basename and dirname

Linux Basics: Split Path Strings with basename and dirname

October 27, 2026

A report generator may need the final path component or its containing directory. GNU basename and dirname transform path strings without opening the named file. A few edge cases make their behavior easier to use in shell scripts.

Prepare a scratch directory

Tested on Linux with GNU coreutils 9.7 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a separate temporary fixture directory and removes only that directory on exit. It does not operate on your existing files.

Keep using the same terminal. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-basename-dirname.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
path='reports/monthly report.txt'
printf 'Final component: %s\n' "$(basename -- "$path")"
printf 'Directory component: %s\n' "$(dirname -- "$path")"
printf 'Suffix removed: %s\n' "$(basename -- "$path" .txt)"
printf 'Bare filename directory: %s\n' "$(dirname -- notes.txt)"
printf 'Trailing slash removed: %s\n' "$(basename -- reports/2026/)"
printf 'Root basename: %s\n' "$(basename -- /)"
test ! -e reports
printf 'Paths need not exist: verified\n'

Run it:

bash demonstrate-basename-dirname.sh

Observed output:

Final component: monthly report.txt
Directory component: reports
Suffix removed: monthly report
Bare filename directory: .
Trailing slash removed: 2026
Root basename: /
Paths need not exist: verified

Read the result

Quoting preserves the space in the example path. basename can remove an exact suffix from the final component, while dirname returns . for a name without a directory part. Trailing separators are handled as part of lexical path processing. None of these calls requires the file or directory to exist.

Apply it carefully

These commands do not resolve symbolic links, normalize a path against a working directory, or enforce directory containment. The suffix argument is literal, not a regular expression. Command substitution strips trailing newline bytes, so it cannot safely preserve every legal Unix filename; use an appropriate NUL-delimited interface when processing arbitrary filenames in bulk. Do not turn a display-friendly basename into an authorization decision.

The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.

Clean up

Remove the source and then the empty practice directory:

rm -- demonstrate-basename-dirname.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect the remaining files rather than deleting unknown contents.

Acknowledgements

This post was created with assistance from AI.

Last updated on