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?


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

  • 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%.*} 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.. Oct 12 '13 at 18:27
  • @sfrj , are you facing any issue ? Oct 12 '13 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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