Basename Explained

basename
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

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[1] and UnxUtils[2] and is in IBM i.[3]

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.

$ 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 .extfilename

$ basename /path/to/filename.ext xxfilename.ext

Notes and References

  1. Web site: CoreUtils for Windows . 2025-08-09 . 2025-07-30 . https://web.archive.org/web/20250730094951/https://gnuwin32.sourceforge.net/packages/coreutils.htm . live .
  2. Web site: Native Win32 ports of some GNU utilities . 2020-07-25 . 2006-02-09 . https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/ . live .
  3. Web site: IBM System i Version 7.2 Programming Qshell . en . IBM . . IBM . 2020-09-05 . live. https://web.archive.org/web/20200918130823/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc . 2020-09-18 .