Linux Comm Basename
# Linux basename Command
[ Linux Command Manual](#)
* * *
## What is the basename Command
`basename` is a simple but practical command-line tool in Linux systems, used to extract the filename portion from a file path. It acts like a path parser, capable of stripping directory information and keeping only the final filename.
**Analogy**: Imagine you have a complete mailing address (e.g., "China/Beijing/Haidian/Zhongguancun Street No. 1"), the function of `basename` is to extract only the final "Zhongguancun Street No. 1" part.
* * *
## Basic Syntax of basename Command
basename path
### Parameter Description
1. **path** (required): the full path string to be processed
2. **suffix** (optional): if specified, this suffix will be removed from the result
### Common Options
| Option | Description |
| --- | --- |
| `-a` | support multiple paths as parameters |
| `-s suffix` | remove the specified suffix (equivalent to adding suffix parameter at the end of command) |
| `-z` | use null character to separate output results (instead of newline) |
* * *
## Basic Usage Examples
### Example 1: Basic Filename Extraction
## Example
$ basename/home/user/documents/report.txt
report.txt
### Example 2: Remove File Extension
## Example
$ basename/home/user/documents/report.txt .txt
report
### Example 3: Process Multiple Files (using -a option)
## Example
$ basename-a/path/to/file1.txt /another/path/file2.log
file1.txt
file2.log
* * *
## Practical Application Scenarios
### Scenario 1: Batch Processing File Extensions
## Example
for file in*.jpg; do
mv"$file""$(basename "$file" .jpg).png"
done
**Explanation**: This script will rename all .jpg files in the current directory to .png files, only modifying the extension while keeping the original filename.
### Scenario 2: Get Current Script Name in Scripts
## Example
#!/bin/bash
SCRIPT_NAME=$(basename"$0")
echo"Running script: $SCRIPT_NAME"
### Scenario 3: Combined with find Command
## Example
find/var/log -name"*.log"-exec basename{} ;
**Function**: Find all .log files under /var/log directory and display only filenames (without paths)
* * *
## FAQ
### Q1: What is the difference between basename and dirname?
| Command | Function | Example Input | Example Output |
| --- | --- | --- | --- |
| `basename` | extract last part of path | /a/b/c.txt | c.txt |
| `dirname` | extract directory part | /a/b/c.txt | /a/b |
### Q2: Why doesn't my basename command work?
Common reasons:
1. Path contains special characters (like spaces) but not quoted
* **Wrong**: `basename /path/with spaces/file`
* **Correct**: `basename "/path/with spaces/file"`
2. Variables not quoted when used in scripts
* **Wrong**: `basename $filepath`
* **Correct**: `basename "$filepath"`
* * *
## Advanced Tips
### Tip 1: Handle Multiple Suffixes
## Example
$ basename archive.tar.gz .gz
archive.tar
$ basename archive.tar.gz .tar.gz
archive
### Tip 2: Use in Pipes
## Example
echo"/usr/local/bin/python"|xargs basename
Output: `python`
### Tip 3: Alternative Using Parameter Expansion
In Bash, you can also use parameter expansion to achieve similar functionality:
## Example
path="/home/user/file.txt"
filename="${path##*/}"# equivalent to basename
echo"$filename"
* * Linux Command Manual](#)
YouTip