← All commands
THE COMMAND REFERENCE

basename

Print the file name part of a path, with or without its extension.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

basename removes the folders from a path and prints the last part. It can also remove a suffix, such as an extension. It works on the text of the path only, so the file does not have to exist.

Background

The basename and dirname utilities first appeared in 4.4BSD, according to the macOS manual. Today the name covers GNU basename, the basename that macOS ships, and the basename applet in BusyBox. They agree on a path, and on a suffix given as a second argument; they differ in the options for several names at once.

Examples

Print the file name of a path:

basename logs/server.log

Remove a suffix as well:

basename logs/server.log .log

A trailing slash is ignored:

basename /usr/local/bin/

Use it in a loop to build new names from old ones:

for f in logs/*.log; do echo "$(basename "$f" .log).txt"; done

Common mistakes

basename never looks at the file system: basename logs/missing.log prints missing.log. Quote a path with spaces, or it is split into several arguments. A suffix only matches at the very end, and a name that equals the suffix is left whole, so basename .log .log prints .log. In a shell script, ${path##*/} does the same without starting a process, but it is not what a trailing slash gives you.

Portability

Only a path with an optional suffix is in the Common examples. -a and -s for several names differ between implementations and are not in POSIX. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox basename

BusyBox bundles basename as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox basename prints a name with an optional suffix; -a and -s depend on the build.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.