Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux basename command

Reference for the Linux basename command: extract the file name from a path, strip a suffix, handle several paths with -a and use parameter expansion instead.

basename prints the last component of a path, the "file name" part: basename /var/log/syslog prints syslog. Trailing slashes are ignored, so basename /usr/lib/ prints lib. You can also give a suffix to remove, which makes it handy for changing extensions.

basename works on the text of the path only. It does not look at the filesystem, so the path does not have to exist. Its partner is dirname, which prints everything before the last component.

Terminal
basename NAME [SUFFIX]
basename OPTION... NAME...

Try it

Bash Names and suffixes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Several names, -s, and a loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
basename NAME [SUFFIX]
basename [-a] [-s SUFFIX] [-z] NAME...
Name Description
NAME The path to shorten.
SUFFIX An optional ending to remove from the result (second positional argument form).
-a, --multiple Accept several names, one result per name.
-s SUFFIX, --suffix=SUFFIX Remove SUFFIX from every name; implies -a.
-z, --zero End output lines with NUL instead of newline.

Returns: the shortened name on standard output; exit status 0, or non-zero on a usage error such as a missing operand.

Usage notes

  • Suffix removal is literal, not a pattern: basename file.txt .t* does not match a wildcard. The suffix must match the exact end of the name.
  • The suffix is never removed if it would leave nothing: basename .bashrc .bashrc prints .bashrc.
  • Trailing slashes are dropped (dir/ gives dir); / gives /. An empty string gives an empty line.
  • Only the last extension is handled by one suffix. For archive.tar.gz, use .tar.gz to remove both.
  • Quote the argument. basename $path breaks on spaces. Use basename -- "$path" if the name may begin with a dash.
  • Performance: $(basename "$f") starts a process per call. In loops over many files, parameter expansion is far faster: ${f##*/} is the base name, ${f%.*} removes the last extension.
  • Difference from parameter expansion: ${path##*/} of /usr/lib/ is empty (it takes everything after the last slash), whereas basename prints lib. Strip trailing slashes first if that matters.
  • With find: find . -type f -exec basename {} \; lists names without directories; GNU find also offers -printf '%f\n'.
  • Newlines in names break line-based processing; use -z with xargs -0.