Reputation:
I want to delete the old log files in log directory. To delete the log files which are more than 6 months, I have written the script like
find /dnbusr1/ghmil/BDELogs/import -type f -mtime +120 -exec rm -f {} \;
By using this script i am able to delete the old files, but how do I invoke this script by using java?
Upvotes: 1
Views: 1112
Reputation: 64026
If portability is an issue, preventing you from running with Runtime.exec(), this code is quite trivial to write in Java using File and a FilenameFilter.
Edit: Here is a static method to delete a directory tree... you can add in the filtering logic easily enough:
static public int deleteDirectory(File dir, boolean slf) {
File[] dc; // directory contents
String dp; // directory path
int oc=0; // object count
if(dir.exists()) {
dir=dir.getAbsoluteFile();
if(!dir.canWrite()) {
throw new IoEscape(IoEscape.NOTAUT,"Not authorized to delete directory '"+dir+"'.");
}
dp=dir.getPath();
if(dp.equals("/") || dp.equals("\\") || (dp.length()==3 && dp.charAt(1)==':' && (dp.charAt(2)=='/' || dp.charAt(2)=='\\'))) {
// Prevent deletion of the root directory just as a basic restriction
throw new IoEscape(IoEscape.GENERAL,"Cannot delete root directory '"+dp+"' using IoUtil.deleteDirectory().");
}
if((dc=dir.listFiles())!=null) {
for(int xa=0; xa<dc.length; xa++) {
if(dc[xa].isDirectory()) {
oc+=deleteDirectory(dc[xa]);
if(!dc[xa].delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete directory '"+dc[xa]+"' - it may not be empty, may be in use as a current directory, or may have restricted access."); }
}
else {
if(!dc[xa].delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete file '"+dc[xa]+"' - it may be currently in use by a program, or have restricted access."); }
}
oc++;
}
}
if(slf) {
if(!dir.delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete directory '"+dir+"' - it may not be empty, may be in use as a current directory, or may have restricted access."); }
oc++;
}
}
return oc;
}
Upvotes: 3
Reputation: 12257
When you only want to call the command you described call:
Runtime r = Runtime.getRuntime();
Process process = r.exec("find /dnbusr1/ghmil/BDELogs/import -type f -mtime +120 -exec rm -f {} \\;"); //$NON-NLS-1$
process.waitFor();
If you want to call more than one command use Chris Jester-Young answer. The exec method can also define files you want to use. The other answers link the method describtion.
Upvotes: 2
Reputation: 223023
Adding to Crashworks's answer, you call with these arguments in the cmdarray
:
new String[] {"find", "/dnbusr1/ghmil/BDELogs/import", "-type", "f",
"-mtime", "+120", "-exec", "rm", "-f", "{}", ";"}
If your find
supports the -exec ... {} +
syntax, change the ";"
at the end to "+"
. It will make your command run faster (it will call rm
on a batch of files at once, rather than once for each file).
Upvotes: 1
Reputation: 143134
Using system calls in Java is possible, but is generally a bad idea because you'll lose the portability of the code. You could look into Ant and use something like this purge task.
Upvotes: 1