WillB WillB - 9 months ago 49
Bash Question

Linux: Rename File With Exact Substring

I have a JAR file that comes with its version number appended in the file name, and I'd like to rename it to something generic in my crontab shell script every time it rebuilds from an external repository. For example: "example-1.9.4.jar" should be renamed to "example.jar" so all versions share the same filename (no more than one version exists at a time in any given directory). I was thinking of using Regex with the rename command but I can't figure out how to handle exact substring patterns.

How do I use the rename command to remove the version number from the filename?


Using rename utility you can do:

rename -n 's/-(\d+\.)+jar$/.jar/' *.jar

If rename isn't available then use:

for f in *.jar; do
    mv "$f" "${f/-[0-9]*jar/.jar}"