{{Short description|Shell command for extracting the directory path portion from a path}} {{Lowercase title}} {{Infobox software | 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 = }} '''<code>dirname</code>''' 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.<ref>{{man|1|dirname|Linux}}</ref> The command is available for Windows as part of the GnuWin32 project<ref>{{Cite web |url=https://gnuwin32.sourceforge.net/packages/coreutils.htm |title=CoreUtils for Windows |access-date=2026-02-03 |archive-date=2025-07-30 |archive-url=https://web.archive.org/web/20250730094951/https://gnuwin32.sourceforge.net/packages/coreutils.htm |url-status=live }}</ref> and UnxUtils<ref>{{Cite web |url=http://unxutils.sourceforge.net/ |title=Native Win32 ports of some GNU utilities |access-date=2022-02-23 |archive-date=2006-02-09 |archive-url=https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/ |url-status=live }}</ref> and is in IBM i.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 |url-status=live|archive-url=https://web.archive.org/web/20200918130823/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |archive-date=2020-09-18 }}</ref>
== Usage == The Single UNIX Specification is: {{code|dirname path}}. The required argument, <code>path</code>, is a file path string.
== Examples == The command reports the directory path portion of a path ignoring any trailing slashes.
<syntaxhighlight lang="console"> $ dirname /path/to/filename.ext /path/to
$ dirname /path/to/ /path
$ dirname filename.ext . </syntaxhighlight>
== Performance == Since the command accepts only one operand, its usage within the inner loop of a shell script can be detrimental to performance. Consider:
<syntaxhighlight lang="bash"> while read file; do dirname "$file" done < some-input </syntaxhighlight>
The above causes a separate process invocation for each line of input. For this reason, shell substitution is typically used instead:
<syntaxhighlight lang="bash"> echo "${file%/*}"; </syntaxhighlight>
Or, if relative pathnames need to be handled as well:
<syntaxhighlight lang="bash"> if [ -n "${file##*/*}" ]; then echo "." else echo "${file%/*}"; fi </syntaxhighlight>
Note that these handle trailing slashes differently than {{code|dirname}}.
== See also == * {{Annotated link|basename}} * {{Annotated link|List of POSIX commands}}
== References == {{Reflist}}
== External links == {{Wikibooks|Guide to Unix|Commands}} * {{man|cu|dirname|SUS|return the directory portion of a pathname}} * {{man|1|dirname|die.net}} * {{man|1|dirname|OpenBSD}}
{{Unix commands}} {{Core Utilities commands}}
Dirname Category:Unix SUS2008 utilities Category:IBM i Qshell commands