File Operations

basename

Strip directory and optional suffix from a file path to get the filename.

Synopsis

syntax
basename NAME [SUFFIX]

Examples

Extract filename: doc.txt
basename /home/user/doc.txt
Strip suffix: doc
basename /home/user/doc.txt .txt
Process multiple paths
basename -a /a/b.c /d/e.f

Common options

FlagDescription
-aSupport multiple arguments
-sRemove a trailing suffix
-zSeparate output with NUL, not newline

About basename

The `basename` command strip directory and optional suffix from a file path to get the filename. File operation commands are essential for navigating and managing the Linux filesystem.

They form the backbone of everyday system administration and development workflows. Mastering these commands enables you to efficiently create, move, copy, and organize files and directories from the terminal, which is often faster and more scriptable than graphical alternatives.

The command accepts 3 commonly used flags shown above, though the full set of options is available in the man page (`man basename`). The 3 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Operations Commands

Other commands in the File Operations category

Related tools