basename Command
Beginner Shell Scripting man(1)Strip directory and suffix from filenames
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
basename NAME [SUFFIX]
What Does basename Do?
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.
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.
Options & Flags
| Option | Description | Example |
|---|---|---|
| PATH | Strip directory from path | basename /var/log/syslog |
| PATH SUFFIX | Strip directory and suffix | basename /home/user/photo.jpg .jpg |
| -s | Remove suffix (alternative syntax) | basename -s .tar.gz archive.tar.gz |
| -a | Process multiple arguments | basename -a /path/file1 /path/file2 |
Practical Examples
#1 Get filename from path
Extracts just the filename from the full path.
$ basename /var/log/nginx/access.log
Output:
access.log
#2 Remove extension
Strips the .pdf extension from the filename.
$ basename report.pdf .pdf
Output:
report
#3 In a script
Gets base name without extension for each CSV file.
$ for f in /data/*.csv; do echo "Processing $(basename "$f" .csv)"; done
Output:
Processing users\nProcessing orders
#4 Remove compound extension
Strips a multi-part extension.
$ basename -s .tar.gz backup.tar.gz
Output:
backup
#5 Multiple paths
Extracts filenames from multiple paths.
$ basename -a /usr/bin/python3 /usr/bin/node /usr/bin/php
Output:
python3
node
php
#6 Script variable
Gets the script's own filename — common for usage messages.
$ SCRIPT_NAME=$(basename "$0")Tips & Best Practices
Use in conversion scripts: base=$(basename "$file" .jpg); convert "$file" "${base}.png" — converts image format using the original base name.
dirname is the complement: basename gets the filename; dirname gets the directory. Together they split a path: dirname /a/b/c → /a/b, basename /a/b/c → c.
Quote your variables: Always quote: basename "$filepath". Filenames with spaces will break without quotes.
Frequently Asked Questions
How do I get just the filename from a path?
Use basename /path/to/file. It returns just the filename without the directory.
How do I remove a file extension?
Use basename filename .ext or basename -s .ext filename. For example: basename photo.jpg .jpg returns photo.
What is the difference between basename and dirname?
basename returns the filename (last component). dirname returns the directory path (everything except the last component).
Related Commands
More Shell Scripting Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →