Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2013
    Posts
    4

    Unhappy Unanswered: Script to remove lower version jar files

    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
    adepu

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    I think that the solution lies in removing ".jar" from the file name first, then sorting the file names in ascending order, and keeping only the last of each.
    Doing this will make xyz.1.1.1 larger than xyz.1.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •