Title: change file extension in unix
Tags: unix;file-extension
Question: I am taking a intro to Unix class and am stuck on the final assignment.  I need to write a script to change the file extension of a filename that is input when the script is run.  The new file extension is also input when the script is run.  The script is call chExt1.sh . Our first trial of the script is run as follows
./chExt1.sh cpp aardvark.CPP
The script is suppose to change the second input file extension to the file extension given in the first input.  It is not suppose to matter what file extension is given with the file name or what file extension is given as the new extension, nor is it only for changing uppercase to lowercase.  In hope to make this very clear if given the following:
./chExt1.sh istink helpme.plEaSe
The script would change helpme.plEaSe to helpme.istink .  I have searched on this forum and in google and have had no look with trying the different examples I found.  Below is some of the examples I have tried and what I currently have.
Current
#!/bin/sh
fileExtension="$1"
oldName="$2"
newName=${oldName%%.*}${fileExtension}
echo $newName
The echo is just to see if it works, and if I get it working I'm going to add an mv to save it.
Others that I have tried:
newName=`${oldName%.*}`
newName=`${oldName#.*}`
sed 's/\.*//' $oldName &gt; $newName
I can't seem to find some of the other sed I have used but they involved alot of backslashes and () with .* in there.  I did not try the basename command cause I don't know the file extension to be entered and all I the examples I saw required that you specify what you wanted removed and I can't.  I did not list all the different quote variations that I used but I have tried alot.  My instructions say to use the sed command since we should know how to use that from class but when I try to do it I don't isolate just the ending of the file and I believe (cause it takes so long to finish) that it is going through the whole file and looking for .'s and anything after cause I kept doing .* as the pattern.  Thanks for anyhelp you can give.
Comment: [man rename](https://www.google.com/search?q=man+rename&oq=man+rename&aqs=chrome.0.57j60l3j65l2.2208&sourceid=chrome&ie=UTF-8)

Comment: sample `rename -n s/\.txt$/\.aaa/  *.txt`

Here is the accepted answer: shift shifts the positional parameters, so after calling shift the second parameter ($2) is now the first ($1). The second shift is not necessary, because you are done accessing the parameters. You need to either remove the shift
#!/bin/sh
fileExtension="$1"
oldName="$2"
newName=${oldName%%.*}${fileExtension}
echo $newName
or change $2 to $1.
#!/bin/sh
fileExtension="$1"
oldName="$1"
newName=${oldName%%.*}${fileExtension}
echo $newName
However, you are still missing a dot from your new file name. That is left as an exercise for the reader.
Comment for this answer: Thank you very much, I tried just deleting the second shift but that didn't work.  But when I changed the $2 to $1 it worked great.  And thanks for pointing out that I was missing the dot, I probably would've gone all the way through the assignment without catching that.
Comment for this answer: "I tried deleting the second shift". The second shift has no effect, you can keep it or delete it; it doesn't matter either way. The solution is to change $2 to $1 or to delete the _first_ shift. If your problem is solved, please select my answer.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/29/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<unix><file-extension>', 'Title': 'change file extension in unix', 'ViewCount': 2674, 'id': 13618145, 'user_AboutMe': '', 'user_AccountId': 939320.0, 'user_DisplayName': 'gimpdogg', 'user_Reputation': 23.0, 'user_Views': 7.0, 'user_id': 967688.0}}