The basename
command is used to strip the directory and suffix from file names, leaving only the base file name. This is particularly useful when you need to manipulate or display just the file name without its path or extension.
The basic syntax of the basename
command is as follows:
basename [options] [arguments]
-s, --suffix
: Remove a specified suffix from the file name.-a, --multiple
: Process multiple files and return their base names.Here are some practical examples of using the basename
command:
Extracting the base name from a file path:
basename /usr/local/bin/script.sh
Output:
script.sh
Removing a suffix from a file name:
basename report.txt .txt
Output:
report
Processing multiple files:
basename -a /home/user/file1.txt /home/user/file2.txt
Output:
file1.txt
file2.txt
Using basename with a custom suffix:
basename /var/log/syslog.log .log
Output:
syslog
basename
will return the full file name.-a
option to handle multiple files in a single command, which can save time and effort.basename
in scripts where you need to extract file names for logging or processing purposes.