Search code examples
linuxbashpattern-matchingrenamefilenames

Remove "-template" part from multiple filenames in linux


I have some files with names like:

jcr-repository-template.xml
imapserver-template.xml

and I would like to rename them as:

jcr-repository.xml
imapserver.xml.

I tried to use find -name "*-template.*" -exec rename -v "template" "" {} ";"

It worked but I only matched the "template" and not "-template" (I couldn't find how). So now I have one dash character at the end of each filename. Can anyone help me with that?


Solution

  • In many commands option -- can be used to indicate the end of options i could test and it worked:

    touch imapserver-template.xml
    rename -v -- -template '' imapserver-template.xml
    

    otherwise another solution can be to use an equivalent regex pattern which doesn't start with -

    rename -v '(?:-template)' '' imapserver-template.xml