The basename
command in the Debian Almquist Shell (dash) is used to strip the directory and suffix from file names. It is particularly useful when you want to obtain just the file name from a full path or remove a specific suffix from a file name.
The basic syntax of the basename
command is as follows:
basename [options] [arguments]
-a
: Treat each argument as a separate file name and return the base name for each.-s
: Remove a specified suffix from the file name.--help
: Display help information about the command.basename /usr/local/bin/script.sh
Output:
script.sh
basename report.txt .txt
Output:
report
basename -a /path/to/file1.txt /path/to/file2.log
Output:
file1.txt
file2.log
basename -s .log /path/to/file2.log
Output:
file2
basename
in scripts to dynamically extract file names from paths, which can be useful for logging or processing files.basename
with other commands like find
to manipulate file names effectively.basename
only returns the last component of a path; if you need to work with directories, consider using the dirname
command.