File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Passing wilcard * in Runtime.exec() command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing wilcard * in Runtime.exec() command" Watch "Passing wilcard * in Runtime.exec() command" New topic
Author

Passing wilcard * in Runtime.exec() command

Srikanth Kumar
Ranch Hand

Joined: Jun 04, 2008
Posts: 36
Hey all,
I need to delete all the files that starts with ABC. For this now i'm using Runtime.exec and passing the parameter "rm -r /usr/tmp/ABC*". But amazingly it does not delete the directories. Infact if i try to delete a directory in one attempt then it seems its ok like if i pass "rm -r /usr/tmp/ABC-123".
Why the Runtime.exec() is not recognizing/allowing to pass wildcard characters? Or is there any other way to do so.

Thanks in advance.


Srikanth Kumar
SCJP 5, SCWCD 5, SCDJWS 5
Srikanth Kumar
Ranch Hand

Joined: Jun 04, 2008
Posts: 36
I am using Linux/Unix machine(s)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

When you type a wildcard at a UNIX command prompt, it's the shell program (i.e., bash) that interprets it. When you use Runtime.exec() to execute rm, there's no shell involved. rm gets the asterisk character itself, and doesn't treat it as a special character.

Run a shell, and use the shell to run rm; i.e., like

Runtime.exec(new String[] { "sh", "-c", "rm /tmp/ABC*" });


[Jess in Action][AskingGoodQuestions]
Andrei Matyas
Greenhorn

Joined: Apr 15, 2007
Posts: 25
On linux you should do something like this :

Zemian Deng
Greenhorn

Joined: Jun 12, 2007
Posts: 21
Srikanth,

I hope you just use "rm" as example to use System.exec, as it not portable to other OS systems. Using java.io.File#delete is more safe. Of course you will have to recurse and make sure all files are delete before a directory is to be removed as well.
 
 
subject: Passing wilcard * in Runtime.exec() command