[Solved] remove file extension

Hi,

I have some files with some extension e.g. abc.xml.REMOVE,xyz.xml,efg.xml.REMOVE .

I have to remove the .REMOVE extension. I can display it using the below script but cannot rename it.

ls -l|sed 's/\.REMOVE//'

How can I rename this?

Thanks in advance

try this...

ls -l | sed 's/\.[a-z]*$//'
for i in *.REMOVE
do
 echo mv "$i" "${i%.REMOVE}"
done

Remove the echo when the mv command lines seem alright.

1 Like

Don't you have rename on your system?

rename 's/\.REMOVE$//' *.REMOVE

--
Bye

#!/bin/bash

##This Script will search for a pattern in the file names of source folder,rename the files and put into the target folder.

Source_folder="Give Source Folder Name Here"
Target_fodler="Give Target Folder Name Here"
Remove_Phase="\.REMOVE"
Put_Phase="Modified"

#########################
#Remove check Type 
# 1= Remove from Last
# 2= Remove from anywhere
# 3= Remove from begining
########################
Remove_Check_Type="1"

#Creating the file pattern based on Remove Check Type
case $Remove_Check_Type in
  1)
	FilePattern=".*$Remove_Phase$"
	echo "Will replace from end of file name"
	;;
  2)
	FilePattern=".*$Remove_Phase.*"
	echo "Will replace from anywhere of file name"
	;;
  3)
	FilePattern="^$Remove_Phase*"
	echo "Will replace from starting of file name"
	;;
esac

echo "Using FilePattern $FilePattern"

ls -1 $Source_folder | grep $FilePattern | while read FileName
do
	echo $FileName
	NewFileName=`echo $FileName | sed "s/$Remove_Phase/$Put_Phase/g"`
	echo "New File Name: $NewFileName"
	mv $Source_folder/$FileName $Target_fodler/$NewFileName
done

Thanks for your help.

But I have another question we usually

mv original_file_name new_file_name

but above we are using

mv new_file_nameoriginal_file_name 

Kindly tell what is the significance of % here

I think you are referring to

mv "$i" "${i%.REMOVE}"

here he is using ${i%.REMOVE}
This is a string operation to remove .REMOVE from the last

Nope. The syntax is as you've expected. i has the original name and ${i%.REMOVE} gives the new name.

Check the Bash Reference Manual.