basename — Extract Filename from a Path
basename extracts the final component of a path.
It removes the directory portion and returns only the filename.
Optionally, it can remove a specific suffix (such as .php, .log, .sql).
Like dirname, this command works purely on strings.
It does not check whether the file exists.
1. Core Purpose
Given:
/var/www/html/index.php
basename returns:
index.php
If a suffix is provided:
basename /var/www/html/index.php .php
Output:
index
2. Installation
basename is part of GNU coreutils and is installed by default.
Verify:
basename --version
3. Core Syntax
basename [OPTION] NAME [SUFFIX]
NAME→ File path (absolute or relative)SUFFIX→ Optional string to remove from result- Multiple paths supported with
-a
4. Available Options
| Option | Purpose |
|---|---|
| (none) | Default behavior |
-a, --multiple | Process multiple paths |
-s, --suffix=SUFFIX | Remove specified suffix |
-z, --zero | End output with null character |
--help | Show help |
--version | Show version |
5. How Suffix Removal Works
Important rules:
- The suffix must match exactly at the end of the filename.
- It removes only one matching suffix.
- If the suffix does not match, output remains unchanged.
- The suffix is treated as a string (not “file extension aware”).
6. Practical Examples (WordPress VPS Context)
Basic Filename Extraction
basename /var/www/html/wp-config.php
Output:
wp-config.php
Use case: Identify WordPress config filename.
Remove Extension
basename /var/www/html/index.php .php
Output:
index
Use case: Clean label for logs or reports.
Using -s Option
basename -s .log /var/log/nginx/error.log
Output:
error
Use case: Log rotation scripts.
Multiple Files (-a)
basename -a /var/www/html/index.php /var/www/html/wp-config.php
Output:
index.php
wp-config.php
Batch filename extraction.
Multiple Files + Remove Suffix
basename -a -s .php /var/www/html/index.php /var/www/html/wp-config.php
Output:
index
wp-config
Useful for bulk processing WordPress PHP files.
Multi-Extension Archive
basename /home/user/backup.tar.gz .tar.gz
Output:
backup
Removes entire .tar.gz.
Remove Only Final Extension
basename /home/user/backup.tar.gz .gz
Output:
backup.tar
Only .gz removed.
Wrong Suffix (Unchanged)
basename /home/user/db.sql .txt
Output:
db.sql
Suffix must match exactly.
Relative Path
basename ./index.php
Output:
index.php
Works with relative paths.
Path Without Slash
basename file.txt
Output:
file.txt
No directory component required.
With Variable
F=/var/www/html/wp-content/plugins/hello.php
basename "$F" .php
Output:
hello
Always quote variables.
Backup Naming
basename /home/backups/db_2025-10-02.sql .sql
Output:
db_2025-10-02
Useful in cron jobs.
Null-Terminated Output (-z)
basename -z /var/www/html/wp-content/uploads/image.jpg
Output (null-terminated):
image.jpg\0
Safe with:
xargs -0
Filename with Spaces
basename "/home/user/My Documents/report.pdf"
Output:
report.pdf
Quotes prevent word-splitting errors.
Loop Through PHP Files
for f in /var/www/html/*.php; do
basename "$f"
done
Sample output:
index.php
wp-config.php
functions.php
Bulk inspection of WordPress files.
Remove Extension in Loop
for f in /home/backups/*.sql; do
basename "$f" .sql
done
Clean backup names.
Symlink Name
basename /var/www/current
Output:
current
It does not resolve target paths.
Combine with dirname
p=/var/www/html/wp-content/uploads/image.png
echo "$(dirname "$p")/$(basename "$p" .png).webp"
Output:
/var/www/html/wp-content/uploads/image.webp
Convert file extension while preserving directory.
Cron Script Example
F=/home/backups/wp_db_$(date +%F).sql
basename "$F" .sql
Output:
wp_db_2025-10-02
Daily database backup labeling.
Integration with find
find /var/www/html -name "*.php" -exec basename {} \;
Sample output:
index.php
functions.php
admin.php
Collect PHP filenames across WordPress installation.
7. WordPress VPS Use Cases
- Generate clean backup names
- Remove extensions for logging
- Parse plugin or theme filenames
- Build dynamic restore scripts
- Automate report generation
8. Common Mistakes
Not Quoting Variables
Wrong:
basename $file
Correct:
basename "$file"
Expecting Existence Validation
basename does not check file existence.
Incorrect Suffix String
Suffix must match exactly:
.tar.gz ≠ .gz
9. Best Practices
- Always quote variables.
- Prefer
-sfor clarity in scripts. - Combine with
dirnamefor full path splitting. - Use
-zwhen piping intoxargs -0. - Remember: suffix is treated as plain string.
10. basename vs dirname (Conceptual Pair)
| Task | Command |
|---|---|
| - | - |
| Extract directory path | dirname |
| Extract filename | basename |
Example:
file=/var/www/html/index.php
dirname "$file" # /var/www/html
basename "$file" # index.php
Together they split any path cleanly.
11. Cheat Sheet
basename /path/to/file
basename /path/to/file .ext
basename -s .ext file.ext
basename -a file1 file2
basename -a -s .php path1 path2
basename -z /path/file
12. Mini Quiz
- What does
basename /var/www/html/wp-config.phpreturn? - Which option processes multiple paths?
- What happens if the suffix does not match?
- Which option prints null-terminated output?
- How does
basenamecomplementdirname?
Summary
basename is essential for:
- Clean filename extraction
- Script automation
- Backup labeling
- WordPress VPS file handling
Combined with dirname, it provides complete control over path parsing in Linux automation workflows.