Remove extension from filepath, accounting for:
Presence of directory slash
Spaces in filenames and directories
Removes only one extension for multi-extension files
Treats dotfiles as regular files
POSIX-compatible
# Remove extension '$filepath' -> '$stempath'
case "$filepath" in
*/* )
# 'filepath' is a path under some directory
dirpath="${filepath%/*}"
filename="${filepath##*/}"
stem="${filename%.*}"
test -z "$stem" && stem="$filename"
stempath="$dirpath/$stem"
;;
*)
# otherwise, filepath is the filename itself
stempath="${filepath%.*}"
test -z "$stempath" && stempath="$filepath"
;;
esac
Output
Across different test outputs using find -type f -printf '%P\n' | xargs -I{} ./convert.sh {}
:
filepath="$1"
...
echo $filepath
echo $stempath
echo
file.tar.gz
file.tar
file.tar
file
file
file
space file.tar.gz
space file.tar
space file.tar
space file
space file
space file
.dotfile.tar
.dotfile
.dotfile
.dotfile
space dir/file.tar.gz
space dir/file.tar
space dir/file.tar
space dir/file
space dir/file
space dir/file
space dir/space file.tar.gz
space dir/space file.tar
space dir/space file.tar
space dir/space file
space dir/space file
space dir/space file
space dir/.dotfile.tar
space dir/.dotfile
space dir/.dotfile
space dir/.dotfile