Basename: Difference between revisions
imported>Stevebroshar →See also: use Annotated link| |
Consolidate with dirname page: Add IBM i, use neutral pathnames, reorder Plan9/Inferno |
||
| Line 12: | Line 12: | ||
| latest release date = | | latest release date = | ||
| programming language = [[C (programming language)|C]] | | programming language = [[C (programming language)|C]] | ||
| operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]] | | operating system = [[Unix]], [[Unix-like]], [[IBM i]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]] | ||
| platform = [[Cross-platform]] | | platform = [[Cross-platform]] | ||
| genre = [[Command (computing)|Command]] | | genre = [[Command (computing)|Command]] | ||
| Line 20: | Line 20: | ||
'''<code>basename</code>''' is a [[shell (computing)|shell]] [[command (computing)|command]] for extracting the last name of a [[file path]]. | '''<code>basename</code>''' is a [[shell (computing)|shell]] [[command (computing)|command]] for extracting the last name of a [[file path]]. | ||
The command was introduced in [[X/Open]] Portability Guidelines issue 2 of 1987. It was inherited into the first version of [[POSIX]] and the [[Single Unix Specification]].<ref>{{man|cu|basename|SUS}}</ref> It first appeared in 4.4BSD.<ref>{{man|1|basename|FreeBSD}}</ref> The version in [[GNU Core Utilities]] was written by David MacKenzie.<ref>{{man|1|basename|Linux}}</ref> The command is available for [[Windows]] as part of the [[GnuWin32]] project<ref>[ | The command was introduced in [[X/Open]] Portability Guidelines issue 2 of 1987. It was inherited into the first version of [[POSIX]] and the [[Single Unix Specification]].<ref>{{man|cu|basename|SUS}}</ref> It first appeared in 4.4BSD.<ref>{{man|1|basename|FreeBSD}}</ref> The version in [[GNU Core Utilities]] was written by David MacKenzie.<ref>{{man|1|basename|Linux}}</ref> The command is available for [[Windows]] as part of the [[GnuWin32]] project<ref>[https://gnuwin32.sourceforge.net/packages/coreutils.htm CoreUtils for Windows]</ref> and [[UnxUtils]]<ref>[http://unxutils.sourceforge.net/ Native Win32 ports of some GNU utilities]</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> | ||
==Use== | ==Use== | ||
| Line 26: | Line 26: | ||
==Examples== | ==Examples== | ||
The command reports the last part of a path ignoring any trailing slashes. | The command reports the last part of a path ignoring any trailing slashes. | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ basename / | $ basename /path/to/filename.ext | ||
filename.ext | |||
$ basename / | $ basename /path/to/ | ||
to | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 40: | Line 39: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ basename / | $ basename /path/to/filename.ext .ext | ||
filename | |||
$ basename / | $ basename /path/to/filename.ext xx | ||
filename.ext | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 57: | Line 56: | ||
{{Wikibooks|Guide to Unix|Commands}} | {{Wikibooks|Guide to Unix|Commands}} | ||
*{{man|cu|basename|SUS|return non-directory portion of a pathname}} | *{{man|cu|basename|SUS|return non-directory portion of a pathname}} | ||
*{{man|1|basename|die.net}} | |||
*{{man|1|basename|OpenBSD}} | |||
*{{man|1|basename|Plan 9}} | *{{man|1|basename|Plan 9}} | ||
*{{man|1|basename|Inferno}} | *{{man|1|basename|Inferno}} | ||
{{Unix commands}} | {{Unix commands}} | ||
{{Core Utilities commands}} | |||
{{Plan 9 commands}} | {{Plan 9 commands}} | ||
[[Category:Standard Unix programs|Basename]] | [[Category:Standard Unix programs|Basename]] | ||
[[Category:Unix SUS2008 utilities]] | [[Category:Unix SUS2008 utilities]] | ||
[[Category:IBM i Qshell commands]] | |||
[[Category:Plan 9 commands]] | [[Category:Plan 9 commands]] | ||
[[Category:Inferno (operating system) commands]] | [[Category:Inferno (operating system) commands]] | ||
Latest revision as of 00:49, 23 October 2025
Template:Short description
Template:Lowercase
Script error: No such module "Infobox".Template:Template other Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters".
basename is a shell command for extracting the last name of a file path.
The command was introduced in X/Open Portability Guidelines issue 2 of 1987. It was inherited into the first version of POSIX and the Single Unix Specification.[1] It first appeared in 4.4BSD.[2] The version in GNU Core Utilities was written by David MacKenzie.[3] The command is available for Windows as part of the GnuWin32 project[4] and UnxUtils[5] and is in IBM i.[6]
Use
The Single UNIX Specification is: basename path [suffix]. The required argument, path, is a file path string. The second argument, which is optional, is text to remove from the end of the last name if it ends with the text.
Examples
The command reports the last part of a path ignoring any trailing slashes.
$ basename /path/to/filename.ext
filename.ext
$ basename /path/to/
to
If the suffix argument is included and matches the end of the last name, then that text is removed from the result.
$ basename /path/to/filename.ext .ext
filename
$ basename /path/to/filename.ext xx
filename.ext
See also
References
<templatestyles src="Reflist/styles.css" />
- ↑ – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
- ↑ – FreeBSD General Commands Manual
- ↑ – Linux User Manual – User Commands
- ↑ CoreUtils for Windows
- ↑ Native Win32 ports of some GNU utilities
- ↑ Script error: No such module "citation/CS1".
Script error: No such module "Check for unknown parameters".
External links
- : return non-directory portion of a pathname – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
- – Linux User Commands Manual
- – OpenBSD General Commands Manual
- – Plan 9 Programmer's Manual, Volume 1
- – Template:Man/Inferno
Script error: No such module "Navbox". Template:Core Utilities commands Template:Plan 9 commands