I want to get the filename (without extension) and the extension separately.
The best solution I found so far is:
NAME=`echo \"$FILE\" | cut -d\'.\'
You could use the cut command to remove the last two extensions (the ".tar.gz"
part):
$ echo "foo.tar.gz" | cut -d'.' --complement -f2-
foo
As noted by Clayton Hughes in a comment, this will not work for the actual example in the question. So as an alternative I propose using sed
with extended regular expressions, like this:
$ echo "mpc-1.0.1.tar.gz" | sed -r 's/\.[[:alnum:]]+\.[[:alnum:]]+$//'
mpc-1.0.1
It works by removing the last two (alpha-numeric) extensions unconditionally.
[Updated again after comment from Anders Lindahl]