[Solved] remove file extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] remove file extension
# 1  
Old 09-26-2012
[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.
Code:
ls -l|sed 's/\.REMOVE//'

How can I rename this?

Thanks in advance
# 2  
Old 09-26-2012
try this...

Code:
ls -l | sed 's/\.[a-z]*$//'

# 3  
Old 09-26-2012
Code:
for i in *.REMOVE
do
 echo mv "$i" "${i%.REMOVE}"
done

Remove the echo when the mv command lines seem alright.
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 09-26-2012
Don't you have rename on your system?
Code:
rename 's/\.REMOVE$//' *.REMOVE

--
Bye
# 5  
Old 09-26-2012
Code:
#!/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

# 6  
Old 09-26-2012
Thanks for your help.

But I have another question we usually
Code:
mv original_file_name new_file_name

but above we are using
Code:
mv new_file_nameoriginal_file_name

Kindly tell what is the significance of % here
# 7  
Old 09-26-2012
I think you are referring to

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

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Remove the last 15 characters of a filename with respect to leave file extension

how can i remove numbers of characters from the last name of file with respect to not remove the files extension example VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-.mp4 i want to rename this to VFX-Official-Trailer-(HD)-Shhh... (13 Replies)
Discussion started by: ateya
13 Replies

3. Shell Programming and Scripting

[Solved] Replace extension of filename stored in a variable

Hi there, I have a small question (most like a true beginners question :) ). In a script I have a filename stored in variable (vFile). Through the an input parameter this variable gets its value (for instance cookie.txt). Two new variables are created with the value of vFile, but with a... (2 Replies)
Discussion started by: rberkers
2 Replies

4. Shell Programming and Scripting

[Solved] Howto remove extra space in the file

Hi Gurus, I have a file which contains some special char or space. when using cat -evt I can see the file as following: 0,"0000","abc/def aaa ... (6 Replies)
Discussion started by: ken6503
6 Replies

5. Shell Programming and Scripting

Remove the file except with particular extension

Hi all i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone... (5 Replies)
Discussion started by: vikatakavi
5 Replies

6. Shell Programming and Scripting

[Solved] Remove file older than 90 days

I have crontab job a tar file to a directory ( tar -cvf /tmp/backup/or.`date +%m%d%y`. /ora/db/* ) , it will do it every day . Now I don't want to keep too much files , I just want to keep the file for 90 days , can advise if I want to remove the backup file which are elder than 90 days , can... (1 Reply)
Discussion started by: ust3
1 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Help to remove a line from a file

Hi, I just upgraded one my server to latest version RHEL, I have many users who will do SSH from another server. I wanted to update all of the users home directory and remove the security key. For example. /home/XYZ/.ssh/known_hosts and remove this hostsname. Please see below and advise.... (2 Replies)
Discussion started by: samnyc
2 Replies

8. Shell Programming and Scripting

[Solved] Giving files .txt extension

Hi there, I have around 145,000 files with no file extension in this directory - /home/adams/29: The file name varies but all end with a number from 0 - 9, e.g. TTFILE_BAT_235496, CCNHATA_RFC_23455 I want to give all these 145,000 .txt extension. Please how do I do that? Thanks (2 Replies)
Discussion started by: Creems
2 Replies

9. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

10. Shell Programming and Scripting

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... (12 Replies)
Discussion started by: prvnrk
12 Replies
Login or Register to Ask a Question