remove file extension

Hi ALL,

I'm new to this forum.

Thanks and congrats to all for their great efforts building this site simply superb for all unix administrators.

My requirement is to remove extensions of the files in the current directory. I'm doing it using below script which is working but i think it is very inefficient as i use lot of variables.I want to get done by avoiding usage of tempfile file and any other temp files being used.

#!/usr/bin/bash
ls -l |awk '{print $9}' >/list
cat /list |while read line
do
echo $line >/tempfile
first=`cut -f1 -d'.' /tempfile`
echo $first >>/output.txt
mv $line $first
done

Thanks
Praveen RK

ls -1 | sed 's/\(.*\)\..*/\1/'

Perhaps this.

for file in *
do
  if [ -f $file ] ; then
    # name without extension
    name=${file%\.*}
    echo ${name} >> output.txt
  fi ;
done

You really dont need the -1 if you are going to pipe the output to some other command.

Great, Thanks for letting me know :slight_smile:

Thanks Lorcan for your quick reply. The command just displays the files without extension but how to add "mv" most efficiently as i need to rename the files?

Praveen RK

Thanks vino, i got it done.

Lorcan, thanks and ignore my last msg.

Use zsh:

autoload -U zmv
zmv '*' '${f%%\.*}'

So what about if you don't want to remove all file extensions what about

mike.blake.mno.error
mike.blake.mno

and I want to remove the .error on all of them but make sure the other extensions aren't touched?

for file in *
do
if [ -f $file ] ; then
# name without extension
name=${file%\.error*}
mv ${file} ${name}
echo ${name} "complete" >> output.txt
fi ;
done

This is using basename

$ ls
a.s.d.out c.d.f.k.out

$ ls | while read file
> do
> EXTN=`echo a.s.d.out | awk -F "." '{print $NF}'`
> mv $file `basename $file .$EXTN`
> done

$ ls
a.s.d c.d.f.k

/\(.*\)\..*/

Can you explain..how..this removes the extension....what the double dots play here.

I just collect the info before the "." part and save the pattern and display it. So to do that you have to use "( )" operator in sed.

And \..* represents anything that starts with a dot and followed by mutiple chars which is not displlayed or saved either.

Hope its clear...