basename
Strip directory and suffix from filenames.
TLDR
Extract filename from path
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
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
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.
