Basename
Shell command for extracting the last name from a path
title: "Basename" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["standard-unix-programs", "unix-sus2008-utilities", "ibm-i-qshell-commands", "plan-9-commands", "inferno-(operating-system)-commands"] description: "Shell command for extracting the last name from a path" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Basename" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Shell command for extracting the last name from a path ::
::data[format=table title="Infobox software"]
| Field | Value |
|---|---|
| name | basename |
| released | |
| programming language | C |
| operating system | Unix, Unix-like, IBM i, Plan 9, Inferno |
| platform | Cross-platform |
| genre | Command |
| license | coreutils: GPLv3+ |
| Plan 9: MIT License | |
| :: |
| name = basename
| logo =
| screenshot =
| screenshot size =
| caption =
| developer =
| released =
| 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+
Plan 9: MIT License
| website =
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. It first appeared in 4.4BSD. The version in GNU Core Utilities was written by David MacKenzie. The command is available for Windows as part of the GnuWin32 project and UnxUtils and is in IBM i.
Use
The Single UNIX Specification is: . 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.
::code[lang=console] $ 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.
::code[lang=console] $ basename /path/to/filename.ext .ext filename
$ basename /path/to/filename.ext xx filename.ext ::
References
References
- {{man. cu. basename. SUS
- {{man. 1. basename. FreeBSD
- {{man. 1. basename. Linux
- [https://gnuwin32.sourceforge.net/packages/coreutils.htm CoreUtils for Windows]
- [http://unxutils.sourceforge.net/ Native Win32 ports of some GNU utilities]
- IBM. "IBM System i Version 7.2 Programming Qshell".
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::