Home·Terminal commands

basename

Files and directories

Usage

basename [path] [suffix]

Strips the directory part off a path and can drop a suffix too; it is pure string work and never checks whether the file exists.

Common flags

FlagMeaning
-aTreat every argument as its own path
-sStrip the given suffix from each result
-zGNU: end each line with NUL instead of a newline

Examples

basename /var/log/nginx/access.log

Prints access.log.

basename report.tar.gz .gz

Prints report.tar.

basename -s .md -a a.md b.md

Prints a and b.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does basename do?

Strips the directory part off a path and can drop a suffix too; it is pure string work and never checks whether the file exists.

Q. How do I type it?

basename [path] [suffix] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

3 are listed here; the full set is in man basename. This command sits under Files and directories.

Related commands

man page: man basename