user1926453
user1926453

Reputation: 31

Error executing find command through Runtime.getRuntime().exec()

I face an issue while executing the command below from Java

Process p2 = Runtime.getRuntime().exec("find /home/app/reports/ATM.CCC.* -mtime +1000");

I need to find files with the format ATM.CCC. which are older than 1000 days. Also need to delete files older than 100 days.

Upvotes: 0

Views: 119

Answers (1)

Stephen C
Stephen C

Reputation: 719239

I assume that you want /home/app/reports/ATM.CCC.* to be expanded to a list of files.

That won't work because "globbing" is a shell function, and not a core system function performed by the exec syscall ... or the Java exec(...) methods.

What you need to do is run the command in a shell; e.g.

.... exec(new String[]{
             "sh", "-c",
             "find /home/app/reports/ATM.CCC.* -mtime +1000"
          });

Note that you need to use the overload of exec that takes an array of strings. If you attempt to use overload that takes a String and splits it into argument ... like this ...

.... exec("sh -c \"find /home/app/reports/ATM.CCC.* -mtime +1000\"");

the command string won't be split correctly. (The exec splitter doesn't understand Unix / Linux shell-style quoting.)

Upvotes: 2

Related Questions