LinuxCommandLibrary

basename

Strip directory and suffix from filenames.

TLDR

Extract filename from path

$ basename [/path/to/file.txt]
copy
Remove suffix
$ basename [/path/to/file.txt] [.txt]
copy
Multiple files
$ basename -a [/path/to/file1.txt] [/path/to/file2.txt]
copy
Remove any suffix
$ basename -s [.txt] [/path/to/file.txt]
copy

SYNOPSIS

basename path [suffix]

DESCRIPTION

basename removes directory components from a pathname, leaving only the final filename. It can optionally remove a trailing suffix, making it useful for extracting filenames in shell scripts.
The tool is part of GNU coreutils and commonly used in build scripts and file processing pipelines.

PARAMETERS

-a, --multiple

Process multiple arguments
-s, --suffix=suffix
Remove trailing suffix
-z, --zero
Separate output with NUL instead of newline

BEHAVIOR

Given `/path/to/file.txt`:
- basename returns `file.txt`
- basename with suffix `.txt` returns `file`

WORKFLOW

$ # Get filename
basename /usr/local/bin/command
# Output: command

# Remove extension
basename /path/to/document.pdf .pdf
# Output: document

# In scripts
filename=$(basename "$filepath")
name=$(basename "$filepath" .txt)

# Multiple files
basename -a /path/*.txt
copy

CAVEATS

Only removes trailing suffix (not all occurrences). Doesn't handle multiple extensions well (use parameter expansion for that). Path doesn't need to exist.

HISTORY

basename has been part of Unix since the early days, included in POSIX standards, and is available in GNU coreutils since 1992.

SEE ALSO

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community