Created
March 2, 2019 18:12
-
-
Save pdeutsch/e4c731db754bf5bccbf35303bac3be37 to your computer and use it in GitHub Desktop.
Deletes old *.bag files, keeps the last 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
class DeleteOldFiles { | |
public static void main(String[] args) { | |
deleteFiles(); | |
} | |
static void deleteFiles() { | |
File file = new File("/home/lvusers/bags"); | |
File[] files = file.listFiles((path) -> path.getName().endsWith(".bag")); | |
List<File> newFiles = Arrays.stream(files) | |
.sorted((a,b) -> Long.compare(a.lastModified(), b.lastModified())) | |
.limit(3) | |
.collect(Collectors.toList()); | |
Arrays.stream(files) | |
.filter(f -> !newFiles.contains(f)) | |
.forEach(f -> { | |
System.out.println("Deleting: " + f.getName()); | |
f.delete() | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment