Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate jars in a directory

Tags:

duplicates

jar

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.

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:

  • Script is removing xyz-1.1.1.jar file instead of xyz-1.1.jar
  • Script is removing xyz-tom-20120423.jar and xyz-tom-20120410.jar files.
like image 205
Lme Adepu Avatar asked Jul 31 '26 14:07

Lme Adepu


1 Answers

#!/bin/bash
if [ $# == 0 ]; then
        dir='.'
elif [ $# == 1 ]; then 
        dir=$1
else
        echo "Usage: $0 [dir]";
        exit 1;
fi

for lib in `find $dir -name '*.jar'`; do
  for class in `unzip -l $lib | egrep -o '[^ ]*.class$'`; do
    class=`echo $class | sed s/\\\\.class// | sed s/[-.\\/$]/_/g`
    existing=$( eval "echo \$CLS_${class}" )
    if [ -n "$existing" ]; then echo "$lib $existing"; fi
    eval CLS_${class}="\"${lib} ${existing}\""
  done
done | sort | uniq -c | sort -nr

I find this code here

like image 93
Edgard Leal Avatar answered Aug 04 '26 09:08

Edgard Leal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!