Dirname
Shell command for extracting the directory path portion from a path
title: "Dirname" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["standard-unix-programs", "unix-sus2008-utilities", "ibm-i-qshell-commands"] description: "Shell command for extracting the directory path portion from a path" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Dirname" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Shell command for extracting the directory path portion from a path ::
::data[format=table title="Infobox software"]
| Field | Value |
|---|---|
| name | dirname |
| developer | Various open-source and commercial developers |
| programming language | C |
| operating system | Unix, Unix-like, IBM i |
| platform | Cross-platform |
| genre | Command |
| license | coreutils: GPLv3+ |
| :: |
| name = dirname
| logo =
| screenshot =
| screenshot size =
| caption =
| author =
| developer = Various open-source and commercial developers
| released =
| latest release version =
| latest release date =
| programming language = C
| operating system = Unix, Unix-like, IBM i
| platform = Cross-platform
| genre = Command
| license = coreutils: GPLv3+
| website =
dirname is a shell command for extracting the directory path portion of a path, without the last name. The command is specified in the Single UNIX Specification and is primarily used in shell scripts.
The version in GNU Core Utilities was written by David MacKenzie and Jim Meyering. The command is available for Windows as part of the GnuWin32 project and UnxUtils and is in IBM i.
Usage
The Single UNIX Specification is: . The required argument, path, is a file path string.
Examples
The command reports the directory path portion of a path ignoring any trailing slashes.
::code[lang=console] $ dirname /path/to/filename.ext /path/to
$ dirname /path/to/ /path
$ dirname filename.ext . ::
Performance
Since the command accepts only one operand, its usage within the inner loop of a shell script can be detrimental to performance. Consider:
::code[lang=bash] while read file; do dirname "$file" done < some-input ::
The above causes a separate process invocation for each line of input. For this reason, shell substitution is typically used instead:
::code[lang=bash] echo "${file%/*}"; ::
Or, if relative pathnames need to be handled as well:
::code[lang=bash] if [ -n "${file##/}" ]; then echo "." else echo "${file%/*}"; fi ::
Note that these handle trailing slashes differently than .
References
References
- {{man. 1. dirname. Linux
- [https://gnuwin32.sourceforge.net/packages/coreutils.htm CoreUtils for Windows]
- [http://unxutils.sourceforge.net/ Native Win32 ports of some GNU utilities]
- IBM. "IBM System i Version 7.2 Programming Qshell".
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::