dirname — Return the directory components of a path name
Format
dirname pathname
Description
dirname deletes the trailing part of a file name. The result is the path name of the directory that contains the file. This is useful in shell scripts. dirname does not try to validate the path name. For validation, use pathchk.
dirname follows these rules:
- If pathname is //, return it.
- Otherwise, if it is all slashes, return one slash.
- Otherwise, remove all trailing slashes.
- If there are no slashes remaining in pathname, return period (.).
- Otherwise, remove trailing nonslash characters.
- If the remaining string is //, return it.
- Otherwise, remove any trailing slashes.
- If the resulting string is empty, return period (.).
- Otherwise, return the resulting string.
Examples
The command:
dirname src/lib/printf.c
produces:
src/lib
Localization
dirname uses
the following localization environment variables:
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- NLSPATH
See Localization for more information.
Exit values
- 0
- Successful completion
- 1
- Failed
- 2
- Unknown command-line option
Portability
POSIX.2, X/Open Portability Guide, UNIX systems.
Related information
basename, pathchk