*
The moose likes Linux / UNIX and the fly likes script to remove lower version jars in a directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "script to remove lower version jars in a directory" Watch "script to remove lower version jars in a directory" New topic
Author

script to remove lower version jars in a directory

abheeshek reddy
Ranch Hand

Joined: Nov 28, 2008
Posts: 41
hi Guys,

I have a script to remove lower version jars files in a directory.


#!/bin/bash
#in a directory full of jar files, it is common to have multiple
#versions of the same jars. If versions are named with only periods
#and alphanumerics, then this groups the jars by the name, deletes
#all the lesser versions

#get a list of jars like x-1.0.0.RELEASE.jar, get only the jar name
#without version or .jar and get only non unique lines (ie., duplicate
#jars are found)
for PREFIX in `ls *.jar|sed 's/-[0-9\.a-zA-Z]*\.jar//g'|uniq -d`; do
echo $PREFIX
#now do a reverse sorted listing with the jar name and remove the
#top line so that non latest versions are returned
for FILE in `ls -r ${PREFIX}*|sed '1d'`; do
echo " $FILE"
rm -f $FILE
done
done



It has a bug and need your help to figure and fix the bug.

Scenario: Assuming i have two jars file in a directory with names xyz-1.1.jar and xyz-1.1.1.jar
This script is deleting xyz-1.1.1.jar (higher version) instead of lower version.

Thanks
Abhee
abheeshek reddy
Ranch Hand

Joined: Nov 28, 2008
Posts: 41
abheeshek reddy wrote:hi Guys,

I have a script to remove lower version jars files in a directory.


#!/bin/bash
#Script to remove lower version jar files.

for PREFIX in `ls *.jar|sed 's/-[0-9\.\0-9\.a-zA-Z]*\.jar//g'|uniq -d`; do
for FILE in `ls -r ${PREFIX}*|sed '1d'`; do
echo " $FILE"
rm $FILE
done
done




It has a bug and i need your help to figure and fix the bug.

I have below list of Duplicate jar files in a directory.

xyz-1.1.jar
xyz-1.1.1.jar
abc-1.6.jar
abc-1.3.jar
abc-xyz-pqr-1.9.6.jar
abc-xyz-pqr-1.9.2.jar
xyz-tom.jar
xyz-tom-20120423.jar
xyz-tom-20120410.jar
abc-toolkit-1.6-runtime-5.2.0.jar
abc-toolkit-1.6-runtime-5.0.0.jar

The bug is with xyz pattern jar files.
BUG: 1) Script is removing xyz-1.1.1.jar file instead of xyz-1.1.jar
2) Script is removing xyz-tom-20120423.jar and xyz-tom-20120410.jar files.


Thanks
Abhee
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: script to remove lower version jars in a directory