Dirname: Difference between revisions
m formatting |
|||
(40 intermediate revisions by 27 users not shown) | |||
Line 1: | Line 1: | ||
{{Lowercase title}} |
|||
⚫ | '''< |
||
{{Infobox software |
|||
| name = dirname |
|||
| logo = |
|||
| screenshot = Dirname example.png |
|||
| screenshot size = |
|||
| caption = Example of <code>dirname</code> command |
|||
| author = |
|||
| developer = Various [[open-source software|open-source]] and [[commercial software|commercial]] developers |
|||
| released = |
|||
| latest release version = |
|||
| latest release date = |
|||
| operating system = [[Unix]], [[Unix-like]], [[IBM i]] |
|||
| platform = [[Cross-platform]] |
|||
| genre = [[Command (computing)|Command]] |
|||
| license = [[coreutils]]: [[GPLv3+]] |
|||
| website = |
|||
}} |
|||
⚫ | '''<code>dirname</code>''' is a standard [[computer program]] on [[Unix]] and [[Unix-like]] [[operating system]]s. When <code>dirname</code> is given a [[pathname]], it will delete any suffix beginning with the last slash (<code>'/'</code>) character and return the result. <code>dirname</code> is described in the [[Single UNIX Specification]] and is primarily used in [[shell script]]s. |
||
== |
== History == |
||
The version of <code>dirname</code> bundled in [[GNU]] [[coreutils]] was written by David MacKenzie and Jim Meyering.<ref>{{Cite web|url=https://linux.die.net/man/1/dirname|title = Dirname(1) - Linux man page}}</ref> The command is available as a separate package for [[Microsoft Windows]] as part of the [[UnxUtils]] collection of [[Native (computing)|native]] [[Windows API|Win32]] [[porting|ports]] of common GNU Unix-like utilities.<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref> The {{Mono|dirname}} command has also been ported to the [[IBM i]] operating system.<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 == |
|||
⚫ | |||
dirname string |
dirname string |
||
:< |
:<code>string</code> |
||
::A [[pathname]] |
::A [[pathname]] |
||
⚫ | |||
==Example== |
|||
/home/martin/docs |
|||
⚫ | |||
/usr/home/carpetsmoker |
|||
$ dirname /home/martin/docs/. |
|||
/home/martin/docs |
|||
$ dirname /home/martin/docs/ |
|||
/home/martin |
|||
$ dirname base.wiki |
|||
. |
|||
$ dirname / |
|||
/ |
|||
== Performance == |
|||
Since <code>dirname</code> accepts only one operand, its usage within the [[inner loop]] of shell scripts can be detrimental to performance. Consider |
|||
<syntaxhighlight lang="bash"> |
|||
while read file; do |
|||
dirname "$file" |
|||
done < some-input |
|||
</syntaxhighlight> |
|||
The above excerpt would cause a separate process invocation for each line of input. For this reason, shell substitution is typically used instead <!-- invoking an extra program like sed is avoided this way too --> |
|||
<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 dirname. |
|||
== Misconceptions == |
|||
We might think that paths that end in a trailing slash are a directory. But actually, the trailing slash represents all files within the directory. |
|||
/home/martin/docs/. |
|||
{{according to whom|The correct way to represent a path as a directory is with a trailing slash and a period.|date=September 2019}}{{citation needed|date=September 2019}} |
|||
== See also == |
== See also == |
||
* [[List of Unix |
* [[List of Unix commands]] |
||
* |
* [[basename]] |
||
* [[Path (computing)]] |
|||
== References == |
|||
{{Reflist}} |
|||
== External links == |
== External links == |
||
{{Wikibooks|Guide to Unix|Commands}} |
|||
*{{man|cu|dirname|SUS|return the directory portion of a pathname}} |
|||
*{{man| |
* {{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}} |
|||
⚫ | |||
[[Category:Standard Unix programs|Dirname]] |
[[Category:Standard Unix programs|Dirname]] |
||
⚫ | |||
[[Category:IBM i Qshell commands]] |
|||
[[de:Dirname]] |
|||
[[es:Dirname]] |
Latest revision as of 09:24, 28 January 2024
Example of dirname command | |
Developer(s) | Various open-source and commercial developers |
---|---|
Operating system | Unix, Unix-like, IBM i |
Platform | Cross-platform |
Type | Command |
License | coreutils: GPLv3+ |
dirname
is a standard computer program on Unix and Unix-like operating systems. When dirname
is given a pathname, it will delete any suffix beginning with the last slash ('/'
) character and return the result. dirname
is described in the Single UNIX Specification and is primarily used in shell scripts.
History[edit]
The version of dirname
bundled in GNU coreutils was written by David MacKenzie and Jim Meyering.[1] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[2] The dirname command has also been ported to the IBM i operating system.[3]
Usage[edit]
The Single UNIX Specification for dirname
is:
dirname string
string
- A pathname
$ dirname /home/martin/docs/base.wiki /home/martin/docs
$ dirname /home/martin/docs/. /home/martin/docs $ dirname /home/martin/docs/ /home/martin $ dirname base.wiki . $ dirname / /
Performance[edit]
Since dirname
accepts only one operand, its usage within the inner loop of shell scripts can be detrimental to performance. Consider
while read file; do
dirname "$file"
done < some-input
The above excerpt would cause a separate process invocation for each line of input. For this reason, shell substitution is typically used instead
echo "${file%/*}";
or if relative pathnames need to be handled as well
if [ -n "${file##*/*}" ]; then
echo "."
else
echo "${file%/*}";
fi
Note that these handle trailing slashes differently than dirname.
Misconceptions[edit]
We might think that paths that end in a trailing slash are a directory. But actually, the trailing slash represents all files within the directory.
/home/martin/docs/.
The correct way to represent a path as a directory is with a trailing slash and a period.[according to whom?][citation needed]
See also[edit]
References[edit]
- ^ "Dirname(1) - Linux man page".
- ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
- ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Archived (PDF) from the original on 2020-09-18. Retrieved 2020-09-05.
External links[edit]
- The Single UNIX Specification, Version 4 from The Open Group : return the directory portion of a pathname – Shell and Utilities Reference,
- Linux User Commands Manual –
- OpenBSD General Commands Manual –