# Basename

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Basename
> Markdown URL: https://mediated.wiki/source/Basename.md
> Source: https://en.wikipedia.org/wiki/Basename
> Source revision: 1336331793
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Shell command for extracting the last name from a path}}
{{lowercase}}
{{Infobox software
| name                   = basename
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| developer              = 
| released               = {{Start date and age|1979|1}}
| latest release version = 
| latest release date    = 
| programming language   = [C](/source/C_(programming_language))
| operating system       = [Unix](/source/Unix), [Unix-like](/source/Unix-like), [IBM i](/source/IBM_i), [Plan 9](/source/Plan_9_from_Bell_Labs), [Inferno](/source/Inferno_(operating_system))
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = [coreutils](/source/coreutils): [GPLv3+](/source/GPLv3%2B)<br />Plan 9: [MIT License](/source/MIT_License)
| website                = 
}}
'''<code>basename</code>''' is a [shell](/source/shell_(computing)) [command](/source/command_(computing)) for extracting the last name of a [file path](/source/file_path).

The command was introduced in [X/Open](/source/X%2FOpen) Portability Guidelines issue 2 of 1987. It was inherited into the first version of [POSIX](/source/POSIX) and the [Single Unix Specification](/source/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](/source/GNU_Core_Utilities) was written by David MacKenzie.<ref>{{man|1|basename|Linux}}</ref> The command is available for [Windows](/source/Windows) as part of the [GnuWin32](/source/GnuWin32) project<ref>{{Cite web |url=https://gnuwin32.sourceforge.net/packages/coreutils.htm |title=CoreUtils for Windows |access-date=2025-08-09 |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](/source/UnxUtils)<ref>{{Cite web |url=http://unxutils.sourceforge.net/ |title=Native Win32 ports of some GNU utilities |access-date=2020-07-25 |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](/source/IBM_i).<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=[IBM](/source/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==
The [Single UNIX Specification](/source/Single_UNIX_Specification) is: {{code|basename path [suffix]}}. The required argument, <code>path</code>, 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.

<syntaxhighlight lang="console">
$ basename /path/to/filename.ext 
filename.ext

$ basename /path/to/
to
</syntaxhighlight>

If the suffix argument is included and matches the end of the last name, then that text is removed from the result.

<syntaxhighlight lang="console">
$ basename /path/to/filename.ext .ext
filename

$ basename /path/to/filename.ext xx
filename.ext
</syntaxhighlight>

==See also==
* {{Annotated link|dirname}}
* {{Annotated link|List of POSIX commands}}

==References==
{{Reflist}}

==External links==
{{Wikibooks|Guide to Unix|Commands}}
*{{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|Inferno}}

{{Unix commands}}
{{Core Utilities commands}}
{{Plan 9 commands}}

Basename
Category:Unix SUS2008 utilities
Category:IBM i Qshell commands
Category:Plan 9 commands
Category:Inferno (operating system) commands

---
Adapted from the Wikipedia article [Basename](https://en.wikipedia.org/wiki/Basename) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Basename?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
