Reference articles on history, science, culture and more
Encyclopedia

Basename

Shell command for extracting the last name from a path

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.

01Use

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.

02Examples

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
Watch videos about BasenameExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Basename, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.