• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Passing wilcard * in Runtime.exec() command

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Linux/Unix machine(s)
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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*" });
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On linux you should do something like this :

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic