Last active
May 31, 2020 00:24
-
-
Save seasonedgeek/c6f300c2cdd8a0e87e4e075b9efb58a4 to your computer and use it in GitHub Desktop.
Clean up session logs generated by iTerm.app
This file contains hidden or 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
package com.seasonedgeek.squashlogs; | |
import java.io.File; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* <p> | |
* SquashLogs is a command line process that cleans up dated log files | |
* generated by iTerm (app) sessions. | |
* </p> | |
*/ | |
public class SquashLogs { | |
private static final String LOG_FOLDER = "/Users/tstewart/logs/iterm2/sessions"; | |
/** | |
* <p> | |
* pathToLogFolder - Returns the path to the log files. | |
* </p> | |
* | |
* @return String - location. | |
*/ | |
public String pathToLogFolder() { | |
return LOG_FOLDER; | |
} | |
/** | |
* <p> | |
* listLogFiles - Generates an array of files contained in a directory. | |
* </p> | |
* | |
* @return Array - files. | |
*/ | |
public File[] listLogFiles() { | |
File folder = new File(LOG_FOLDER); | |
return folder.listFiles(); | |
} | |
/** | |
* <p> | |
* deleteLogFiles - Deletes log files whose last modified | |
* dates are older than today's date. | |
* </p> | |
* | |
* @return integer - the number of files deleted. | |
*/ | |
public int deleteLogFiles() { | |
int counter = 0; | |
Calendar time = Calendar.getInstance(); | |
time.add(Calendar.DAY_OF_YEAR, -1); | |
Date today = time.getTime(); | |
File[] files = listLogFiles(); | |
for (File file : files) { | |
if (!file.isDirectory()) { | |
Date lastModified = new Date(file.lastModified()); | |
if (lastModified.before(today)) { | |
counter += (file.delete() ? 1 : 0); | |
} | |
} | |
} | |
return counter; | |
} | |
public static void main( String[] args ) { | |
final String REPORT = "SquashLogs! Number of files squashed? "; | |
StringBuilder message = new StringBuilder(REPORT); | |
SquashLogs squashEm = new SquashLogs(); | |
message.append(squashEm.deleteLogFiles()); | |
System.out.println(message.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment