Community Forums
Connect with us on LinkedIn
Community Notice
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Member
    Join Date
    Jan 2004
    Posts
    755

    Default Delete all but latest version of file?

    Situation:
    In a directory there are the following files:

    filename-1.20070916.1234.txt
    filename-1.20070917.1221.txt
    filename-1.20070918.0501.txt
    filename-1.20070918.1045.txt
    filename-2.20060101.0101.txt
    filename-2.20070101.0101.txt
    filename-3.xml
    filename-3.md5
    filename-3.sha1

    In the above, 20070916 represents the daystamp, 1234 represents the timestamp.

    I need to delete all but the most recent version of each file, so that the directory ends up with the following contents:

    filename-1.20070918.1045.txt
    filename-2.20070101.0101.txt
    filename-3.xml
    filename-3.md5
    filename-3.sha1

    Here's the problem:
    I don't know in advance what filename-1 will be, how many different files there will be, I don't know what the day/time stamps will be, and there will be multiple extensions in use for a given filename.

    I can't use 'delete files older than x' where x is a specific period, such as hours, days, weeks, etc. The latest version may be a year old, it may be 15 minutes newer than the previous version.

    So, I'm getting stumped on where to even begin with this one.

  2. #2
    Member
    Join Date
    Jan 2004
    Posts
    755

    Default

    still working on this if anyone has suggestions....

  3. #3
    Member
    Join Date
    Jan 2004
    Posts
    755

    Default

    In the interest of posting solutions to problems that may prove useful to others, here's the code that a fellow programmer whipped up to solve the problem... it's java rather than a shell script, but perhaps others may be helped:

    Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class SnapshotPurger {
       
       
        private static String[] extensions
            = new String[]{
                "-sources.jar",
                "-sources.jar.md5",
                "-sources.jar.sha1",
                ".pom",
                ".pom.md5",
                ".pom.sha1",
                ".jar",
                ".jar.md5",
                ".jar.sha1",
                ".war",
                ".war.md5",
                ".war.sha1",
                ".ear",
                ".ear.md5",
                ".ear.sha1"
                };
    
        private static String getMatchingExtension(File aFile) {
            for (String ext : extensions) {
                if (aFile.getName().endsWith(ext)) {
                    return ext;
                }
            }
            return null;
        }
       
        public static void purgeStaleSnapshots(File aFile, boolean
    deleteEnabled) throws Exception {
           
           
            Map<String, Set<File>> sortedFiles = new HashMap<String, Set<File>>();
           
            String ext = null;
           
            for (File childFile : aFile.listFiles()) {
                if (childFile.isDirectory()) {
                    purgeStaleSnapshots(childFile, deleteEnabled);
                }
                else {
                    ext = getMatchingExtension(childFile);
                    if (ext != null) {
                        Set<File> files = sortedFiles.get(ext);
                        if (files == null) {
                            files = new HashSet<File>();
                            sortedFiles.put(ext, files);
                        }
                        files.add(childFile);
                    }
                   
                }
               
            }
           
           
            for (Set<File> files : sortedFiles.values()) {
                File mostRecentFile = null;
                for (File file : files) {
                    if (mostRecentFile == null) {
                        mostRecentFile = file;
                    }
                    else if (mostRecentFile.lastModified() >
    file.lastModified()) {
                        System.out.println("Deleting " + file.getAbsolutePath());
                        if (deleteEnabled) {
                            file.delete();
                        }
                    }
                    else {
                       
                        System.out.println("Deleting " + mostRecentFile.getAbsolutePath());
                        if (deleteEnabled) {
                            mostRecentFile.delete();
                        }
                        mostRecentFile = file;
                    }
                }
            }   
           
        }
       
        public static void main(String[] args) throws Exception {
           
            if (args.length == 0) {
                throw new IllegalArgumentException("Snapshot repository path is required.");
            }
           
            File aFile = new File(args[0]);
            if (!aFile.exists()) {
                throw new FileNotFoundException(args[0] + " is not a valid path.");
            }
           
            boolean deleteEnabled = false;
            if (args.length > 1) {
                deleteEnabled = Boolean.parseBoolean(args[1]);
            }
           
            purgeStaleSnapshots(aFile, deleteEnabled);
           
        }
    }

Similar Threads & Tags
Similar threads

  1. Latest Version
    By admins in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 10-30-2006, 04:14 AM
  2. Replies: 2
    Last Post: 02-17-2005, 05:40 PM
  3. Updated to latest version but I'm suddenly a few version back...
    By TheBorg in forum cPanel and WHM Discussions
    Replies: 2
    Last Post: 09-05-2004, 09:00 AM
  4. Module Latest Version Installed Version
    By Helder in forum cPanel and WHM Discussions
    Replies: 0
    Last Post: 07-02-2003, 06:50 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube