Bash comes with commands like basename (extracts filename out of a fullpath filename), and dirname (extracts the directory path out of a fullpath filename). However it doesn’t come with commands to extract the extension. You have to use other commands like cut, awk, rev, or even the bash built in variable tricks to extract the extension.

Sidenote: notice I surround the script with parenthesis, (), that is so a sub-shell is started for the script (anything inside the parenthesis)

Input:

Output:

NOTE: files without extension just produce their filename

The end

Leave a Reply

Your email address will not be published. Required fields are marked *