Results 1 to 2 of 2

Thread: script to remove lower version jars in a directory

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1

    Unhappy script to remove lower version jars in a directory

    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
    Senior Member
    Join Date
    Aug 2012
    Posts
    40,312
    Remove the .jar extensions and then sort the files in descending order. Now ignore the first jar in each group and remove other jars.

    Code:
    > ls -1 *.jar | sed 's/.jar//' | sort -r
    xyz-tom-20120423
    xyz-tom-20120410
    
    xyz-1.1.1
    xyz-1.1
    
    abc-1.6

Similar Threads

  1. Php - Remove Users Script
    By bigdata in forum PHP
    Replies: 0
    Last Post: 05-12-2013, 07:33 PM
  2. Replies: 1
    Last Post: 07-17-2012, 07:00 AM

Bookmarks

Posting Permissions

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