Linux Path Utilities and Shell Globbing
How it works
basename PATH prints the final name component; dirname PATH prints the directory portion. They manipulate path text and do not require the named file to exist. GNU realpath -e PATH resolves an existing path to an absolute canonical path, following symbolic links; every component must exist. Bash filename expansion (globbing) replaces unquoted wildcard patterns with matching pathnames before the command runs. * matches zero or more characters within a component, ? matches one character and [abc] matches one listed character. With normal Bash defaults, a wildcard at the beginning of a component does not match a leading dot, and an unmatched pattern stays literal. Shell options can change these rules.
Prepare a safe practice environment
Use GNU/Linux and Bash as a normal user. From the extracted package root run bash lab-fixtures/files/setup.sh. It prints a fresh private directory under /tmp. Copy that path and enter it with cd (quote the path if needed); check pwd. Each code example below starts in a new lab with the same fixture: notes/hello.txt, notes/daily report.txt, notes/-draft, notes/.hidden, and empty directories archive and empty. Set export LC_ALL=C for the shown ordering and English diagnostics. Never run the modifying examples on your own files.
Separate directory and filename
basename "notes/daily report.txt"dirname "notes/daily report.txt"Prints daily report.txt then notes. The space stays part of the basename.
Resolve a relative path
Prints the lab’s absolute path ending with /notes/hello.txt. The generated prefix varies.
Compare a pattern with literal text
printf '[%s]\n' notes/*.txtprintf '[%s]\n' 'notes/*.txt'The first command prints the two matching text paths as separate arguments, including the name containing a space. The quoted pattern is printed literally. The dot-prefixed fixture and -draft are not matches.
The shell expands the unquoted pattern before printf starts. The filename containing a space is already one matched pathname, so the space does not turn it into two arguments. Quoting the entire pattern prevents filename expansion; use that form when you intend to pass pattern text rather than matched paths.
Common mistakes and safe habits
Globs are not regular expressions. Preview a pattern with printf before using it for any state change. A no-match pattern may remain literal; never assume that an empty set of matches always means zero arguments. Do not use resolved paths alone as a security boundary: filesystem state can change between checks and use.
Try it yourself
Print the basename and dirname of the spaced fixture path. Preview notes/h*.txt and then quote the same pattern. Explain why the results differ.
Use a fresh lab for the practice. When finished, return to the lab root if you are in a subdirectory, then leave it with cd ... Remove only the identified temporary lab folder using your file manager if desired. Never remove an unrelated path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Path utilities and globbing — Knowledge Check
Apply filesystem concepts in a fresh disposable Linux lab.
What does basename notes/hello.txt print?
What does dirname notes/hello.txt print?
What does GNU realpath -e require?
Who expands an unquoted notes/*.txt before printf runs?
What happens to a quoted wildcard in printf?