Bash How to Basename
Command basename strip directory and suffix from filenames. Command syntax:
basename [option] name [suffix]
If the suffix is specified it will remove a trailing suffix. Example:
basename dir1/dir2/dir3/text_file.txt .txt
Output: text_file
Basename takes one argument (filename) and an optional suffix. If you want to give more file names use the option “a” which supports multiple arguments and threat each as “name”.
basename -a /dir/file.txt /dir2/picture.jpg
Output: file.txt picture.jpg
If you want to get the name of your home folder:
basename ~
Often used option is option -s which removes a trailing suffix. Here is an example:
basename -s .txt -a /dir/file.txt /dir2/picture.jpg
file picture.jpg