The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Delete all but latest version of file?

Discussion in 'General Discussion' started by Lyttek, Sep 18, 2007.

  1. Lyttek

    Lyttek Well-Known Member

    Joined:
    Jan 2, 2004
    Messages:
    770
    Likes Received:
    3
    Trophy Points:
    18
    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. :confused: :confused: :(
     
  2. Lyttek

    Lyttek Well-Known Member

    Joined:
    Jan 2, 2004
    Messages:
    770
    Likes Received:
    3
    Trophy Points:
    18
    still working on this if anyone has suggestions....
     
  3. Lyttek

    Lyttek Well-Known Member

    Joined:
    Jan 2, 2004
    Messages:
    770
    Likes Received:
    3
    Trophy Points:
    18
    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);
           
        }
    }
    
     
Loading...

Share This Page