๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

๐Ÿ“œ Shell Scripting September 16, 2026 2

Linux Command: basename

Strip directory and suffix from filenames

Terminal โ€” Shell Scripting
Command
$ basename /var/log/nginx/access.log
Output
access.log

basename strips directory paths and optionally file extensions from filenames. It extracts just the filename component from a full path, making it essential for file processing scripts. basename takes a path and returns only the final component. With a second argument, it also removes a specified suffix (typically the file extension). basename is commonly used in scripts to get filenames from full paths, generate output filenames based on input filenames, and strip extensions for format conversion scripts.

Syntax

basename NAME [SUFFIX]

Key Options

  • PATH โ€” Strip directory from path
  • PATH SUFFIX โ€” Strip directory and suffix
  • -s โ€” Remove suffix (alternative syntax)
  • -a โ€” Process multiple arguments

Examples

Get filename from path

basename /var/log/nginx/access.log

Output: access.log

Remove extension

basename report.pdf .pdf

Output: report

In a script

for f in /data/*.csv; do echo "Processing $(basename "$f" .csv)"; done

Output: Processing users\nProcessing orders

Pro Tips

  • base=$(basename "$file" .jpg); convert "$file" "${base}.png" โ€” converts image format using the original base name.
  • basename gets the filename; dirname gets the directory. Together they split a path: dirname /a/b/c โ†’ /a/b, basename /a/b/c โ†’ c.
  • Always quote: basename "$filepath". Filenames with spaces will break without quotes.

Learn more: Full basename reference โ†’

Share this tip