Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am automating some deployment task with an .sh script but at some point I need to rename a file but it is not working. The file change can be different but the postfix is the same. Let's have a look at this example so you will understand what is that I exactly need:

read folderName
#... at somepoint this scripts creates a file called foldername-1.0-SNAPSHOT.war (I need to rename it)
oldExecutableName ="" + $folderName + "-1.0-SNAPSHOT.war" 
newExecutableName ="" + $folderName + ".war"
[[ -f "${newExecutableName}" ]] &&
mv -v ${oldExecutableName}   ${newExecutableName}


Given input filename

Desired name for file is: filename.war

The text -1.0-SNAPSHOT should be removed from the name of the file

for some reason this is not working, and I think it is because the parameter I am reading from the terminal. Can someboby give me a hand to rename this?

share|improve this question
up vote 1 down vote accepted

You can use :

read -p "Enter Filename :- " finalFinalName

[[ -f "${finalFinalName}" ]] && # if file exists then rename 
mv -v $finalFinalName   ${finalFinalName%$remove_extention}${addExtention}

If there is white space in file then then double quotes the variable eg. "${var}"

${finalFinalName%.*} this will remove extension

share|improve this answer
I made a little mistake in my question. I already have the extension. I need to remove it, that is what I need. – sfrj Oct 12 '13 at 18:13
not an issue.. just small changes required .. ${finalFinalName%.*} – Rahul Patil Oct 12 '13 at 18:14
I think I am close but is not exactly it. Please see the update to my question. I again expressing wrongly. I don't want to say extension, what I want to say is ending(not just after the dot). – sfrj Oct 12 '13 at 18:23
Please check now , I have updated.. – Rahul Patil Oct 12 '13 at 18:27
@sfrj , are you facing any issue ? – Rahul Patil Oct 12 '13 at 18:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.