removing the filename extension

Is there an easy way to strip off a filename's extension?

For example, here's a filename:

blahblahblah.thisisok.thisisnotok

I want to get rid of .thisisnotok from the filename, so that what's left is

blahblahblah.thisisok

Thanks. I have a directory full of filenames that need to be manipulated in this way.

What Operating System and version do you have?
What Shell do you use?

Are the filename extensions random?
Do all files have an extension which is to be removed?

ubuntu 10.10 maverick meerkat

bash shell

all of the files have the same extension that needs to be removed

---------- Post updated at 11:04 AM ---------- Previous update was at 11:02 AM ----------

sorry, and the filenames have spaces in them

Hope this helps point you in the right direction:

for x in *;do mv $x $(echo ${x%*.*});done

If you want to test it first, remove the 'mv' command:

for x in *;do echo ${x%*.*};done

Should list the files with the extension removed. If not, then previous command my require some tweaking. Google "bash shell scripting parameter substitution" for additional help. Good luck.

Posix compatible shells ksh93, bash, ..

filename="some part.some some.filetype"
notlast=${filename%.*}
last=${filename##*.}
first=${filename%%.*}
notfirst=${filename#*.} 

There's no need for the echo command substitution and the first asterisk in your parameter substitution is meaningless since it's a shortest match (% instead of %%). Also, the filenames in question have spaces (perhaps that info was added after you had read the post), so the expansions need to be double quoted to protect them from field splitting.

A slightly simpler version of your approach:

for x in *; do
    mv "$x" "${x%.*}"
done

---------- Post updated at 04:43 PM ---------- Previous update was at 04:40 PM ----------

If it's possible that striping the file extension from file1 can match an existing file, file2, and if you don't want file2 clobbered, you'll want to test for the existence of file2 before doing the mv.

Regards,
Alister