Visit Our UNIX and Linux User Community

Shell script to rename or change file extension case.

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell script to rename or change file extension case.
# 1  
Old 01-06-2010
Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename.


I have files in folder.



and how to rename or change file extension case to

xxxxxxx3.tar or to xxxxxx3.TAR.GZ.

I think if rename works, then it can change lower/uppercase too hopefully.
# 2  
Old 01-06-2010


for name in `ls -1 | grep -v $0`
        file_name=`echo $name|cut -d. -f1 `
        file_ext=`echo $name |cut -d. -f2-3 |tr -t [:lower:] [:upper:]`
        echo mv $name $file_name.$file_ext

just delete the echo and this will do your job.


Last edited by Franklin52; 01-06-2010 at 11:27 AM.. Reason: Please use code tags!
# 3  
Old 01-06-2010
Thanks that is good but maybe someone has more solutions ?
# 4  
Old 01-09-2010


usage () {
  echo "Usage: $0 fromext toext"
  echo "Example: $0 htm html"

if [ -z "$1" ] || [ -z "$2" ]
  echo "Too few arguments."
  exit 0
if [ -n "$3" ]
  echo "Too many arguments."
  exit 0

filelist=`find . -type f -name "*.$fromext"`

changeext () {
  while read file
    newname=`echo "$file" | sed "s/\(.*\.\)${fromext}$/\1$toext/"`
    mv "$file" "$newname"

if [ -n "$filelist" ]
  echo "$filelist" | changeext
  echo "No files to process."

exit 0

Can anyone remove recursive method here and change the file to work like this:

sh failname.txt failname.TXT(whatever extension)

where failname.txt is changing its extension to failname.TXT or failname.mp3

# 5  
Old 01-10-2010
1. what do you mean by remove recursive method ?!

2. If you have the rename command, you can do it very simply.

rename s/.html/.htm/ *.html

# 6  
Old 01-10-2010
I want that script to work file by file.

Not all files. At the moment it changes all png files for example.

I dont want simple solution. I want a bit advanced solution like that script, but without changing all files, but having chance to change file by file.

PS! I need to change whatever file extension to whatever file extension so rename isnt a good one. Ok I can make it rename rename s/.$1l/.$2/ *.$3, but it isnt the problem solved thing.

Thanks. Maybe someone will help me now.

Previous Thread | Next Thread
Test Your Knowledge in Computers #260
Difficulty: Easy
Steve Jobs and Steve Wozniak co-founded Apple in 1976 to sell Wozniak's Apple I personal computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename except dot file extension

After reading the manual of the command rename I would like to apply it to a folder with a couple of files containing old style dots before the file-type, e.g. So I'd like to rename them without the dots in between. Therefore I tried it the following way rename -f -n ... (4 Replies)
Discussion started by: 1in10
4 Replies

2. Shell Programming and Scripting

How to rename the extension of a file?

Hello, I have multiple files named rscclog_2013-03-25.txt;3 in a directory, where 2013-03-25 is the previous day's date and the number after extension .txt preceded by a ';' is any number which i do not know beforehand. Now, i have to rename all such files as rscclog_2013-03-25.txt thus,... (2 Replies)
Discussion started by: rahulkt1987
2 Replies

3. Shell Programming and Scripting

Rename file extension.

I have a list file that contains names of many files. I am reading one file name at a time using for loop Then I like to create one more list file but with the file extension changed to "ctl". Note: The file name can have any number of dots ".". But the extension after the last dot should be... (4 Replies)
Discussion started by: pinnacle
4 Replies

4. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example i want to write a shell script that rename all such file with only single extension like (7 Replies)
Discussion started by: mukulverma2408
7 Replies

5. Shell Programming and Scripting

Rename file to uppercase except extension

Hi, I am trying to make all file in the directory uppercase but not their extension (ex: image.jpg becoming IMAGE.jpg) here is code i am trying. $ ls | while read file do name=${file%%.*} newfilename=$(echo $name | tr 'a-z' 'A-Z') mv $file $newfilename done any suggestions of... (4 Replies)
Discussion started by: johninweb
4 Replies

6. Shell Programming and Scripting

rename a file with new extension

Hi guys, i had many files like filename.20110520_20110519_050030 i have to rename the file by removint the last numerics .. i.e filename.dat i tried with cut command and removed the numerics but i'm not able to add .dat to the files. is there any command insted of cut command to... (1 Reply)
Discussion started by: apple2685
1 Replies

7. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

8. Homework & Coursework Questions

Create file and then change the extension case.

Interpreter should be bash. 1. The problem statement, all variables and given/known data: I need to make a file (myText.txt or song.mp3 or cloud.tar.gz or whatever) and then change the extension to (myText.TXT , song.MP3, cloud.TAR.GZ). It would be good if I can add all information in... (4 Replies)
Discussion started by: Kdenmen
4 Replies

9. Shell Programming and Scripting

rename file extension

I am trying for loop to rename file extension from .txt to .html : as below : for i in *.txt; do mv "$i" `basename $i`.html; done ------------------------------------------- But this renames a file file1.txt as file1.txt.html anyone know how get avoid .html added after .txt ? it... (4 Replies)
Discussion started by: sriram003
4 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies

Featured Tech Videos