{{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 | operating system = Unix, Unix-like, IBM i, Plan 9, Inferno | platform = Cross-platform | genre = Command | license = coreutils: GPLv3+<br />Plan 9: MIT License | website = }} '''<code>basename</code>''' 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.<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>{{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<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.<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== The 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