dirname /path/to/file.txt

Category: Administration

dirname /path/to/file.txt

Print the directory portion of a path

dirname prints everything before the last component, so this yields /path/to. It is the inverse of basename and together they split any path into its two halves. Scripts commonly wrap it around realpath to get an absolute directory.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.