tags:

views:

403

answers:

6
+1  Q: 

Java and unix

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 can able to delete the old files. But how to invoke this script by using java? please any one help me...

+2  A: 

Use java.lang.Runtime.exec() .

Crashworks
If you're going to use that, you might want to read this: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html.
Ben Alpert
@Ben: I disagree with that article. The author of the article, from what I read from page 1, has no idea how Unix processes work. Of course you have to wait for a process before you get the return value! (It's also not the Unix way to wait automatically; the program has to ask for it.)
Chris Jester-Young
@Ben: The 2nd and 3rd pages are better, in talking about the need to handle any input and output the program requires/generates. Those obviously should be set up (perhaps in a separate thread) before waiting.
Chris Jester-Young
@Ben: Overall the article to me seems to touch on Windows programming only. On page 4, the pitfalls about using shell commands and redirections are known to many Unix programmers, who would/should use ['sh', '-c', cmdstring] whenever shell functionality is required.
Chris Jester-Young
Mind you, I will say that for Windows programmers using Java, that article is indeed very useful and should be recommended. So, my comments about its inapplicability to Unix should not in any way be seen to disparage the article in general.
Chris Jester-Young
+1  A: 

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.

Ben Alpert
A: 

This should help Java RunTime

kal
+1  A: 

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).

Chris Jester-Young
+3  A: 

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;
    }
Software Monkey
+2  A: 

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.

Markus Lausberg
Personally I hate shell metacharacter quoting badly enough, just with shell scripts; when I have to do the same with calling system/exec in other languages, I feel the need to rip my eyes out. :-P Hence, I usually prefer the option to specify the argv values explicitly. :-)
Chris Jester-Young