Wildcard Deletion throughJava out-of-process

Posted on 2002-03-04
Last Modified: 2010-03-31
I'm running JDK 1.3 under Unix environment.  If I want to delete all .pdf files in a directory, I can do so on the command line by just typing "rm *.pdf".  But I'm having problems doing this through Java, such as the following:

          String commandString = "rm *.pdf";
          Process p = Runtime.getRuntime().exec(commandString);
      catch (Exception e)

If I specify a file exactly (like a.pdf), it does delete that specific file.  But if I use the wildcard (*), nothing happens.  How can I delete using wildcard?  Thanks in advance.
Question by:jove

Accepted Solution

m_onkey_boy earned 100 total points
ID: 6839904
This should work.  The structure of Wildcard filter may be modified to parse wildcards and such, but this is the general idea....:

File f = new File(strReadDir);
               String[] aList = f.list(new WildcardFilter("pdf"));

for (int i = 0; i < aList.length; i++)

class WildcardFilter implements FilenameFilter {
     private String m_strFileSuffix = null;
     public XsltFilter(String strFileSuffix) {
          m_strFileNameSuffix = strFileSuffix;
     public boolean accept (File f, String strFileName) {
          return strFileName.toLowerCase().endsWith(strFileSuffix);

Expert Comment

ID: 6839922

I gather this comes from the fact you need a shell to interpret the wildcards.

Had you been in an NT environment, with a "del *.pdf" command, I would have suggested to replace it by "start del *.pdf"

I'm sure you can do something similar under Unix, by invoking bash or tcsh and passing it your command-line on the standard input, but I'm not sure of the syntax and haven't got any Unix prompt anywhere next to me. A heavier way to do this would be to have a shell script containing your command, and to call "sh"

Hope this helps,


Expert Comment

ID: 6839929
Oops, concurrent answer !

Sorry m_onkey_boy for my first line about wildcards ;-)
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6839931
On UNIX, it's the shell process which performs the wildcard.  Calling 'rm' directly bypasses this.

MonkeyBoy's suggestion is good for rolling your own wildcard implementation.  

If your goal is to pass commands to Unix, you have to get the Shell to process the arguments.

Example to modify your original post:

         String[] cmd = { "/bin/sh", "-c", "rm *.pdf" };
         Process p = Runtime.getRuntime().exec(cmd);



Expert Comment

ID: 6839989
Another note:  My solution will take a bit longer to write code-wise, but it has the advantages of being platform-independant and faster that calling exec().

The main drawback is that you have to write your own parsing routines that are already in place for you by the OS.

Author Comment

ID: 6839993
Thanks much.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 47
ejb wildfly example 2 16
jmss example java 2 23
java example issue 3 20
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question