1

I need to grep some text inside a list of files(file count is huge) in unix server and then list the file name in a web gui. So I decided best way to achieve this is by writing a unix command executer using Runtime.getRuntime(). Works fine for most of the unix command but facing this strange grep issue.

First of all code:

public class UnixCommandExecutor {

    private static StringBuffer output = new StringBuffer();

    public static String exec(String command) throws Exception{
        Process process = null;
        process = Runtime.getRuntime().exec(command);
        BufferedReader stdErr = getBufferedReader(process.getErrorStream());
        BufferedReader stdIn = getBufferedReader(process.getInputStream());
        StringBuffer data = extractData(stdErr);
        if (data.length() >= 1) {
            System.out.println("Error: " +data.toString());
            throw new Exception(data.toString());
        }
        data = extractData(stdIn);
        if (data.length() >= 1) {
            output = data;
            System.out.println("Output: " +data.toString());
        }
        return output.toString();
    }

    private static BufferedReader getBufferedReader(InputStream stream) {
        InputStreamReader inReader = new InputStreamReader(stream);
        BufferedReader buffReader = new BufferedReader(inReader);
        return buffReader;
    }

    private static StringBuffer extractData(BufferedReader reader)
            throws IOException {
        StringBuffer data = new StringBuffer();
        String s = "";
        while ((s = reader.readLine()) != null) {
            data.append(s + "\n");
        }
        return data;
    }

    public StringBuffer getOutput() {
        return output;
    }
}

Now the call would be something like this: String output = exec("find . -name blah"); This works fine and the result is perfect. Or any other unix command executes and provides the result properly.

But when grep command is used it gives a strange error: String output = exec("grep -l executor *");

Error: grep: *: No such file or directory

This is strange, since if I run this command directly on unix it gives the desired result. Also tried giving the file path something like, String output = exec("grep -l executor /file/path/*"); even then the error is:

Error: grep: /file/path/*: No such file or directory

Any ideas? or any other better way to solve this? Any suggestion is welcome.

1
  • ok.. i got this to work. Basically i needed two thing to make it to work. First use /bin/sh -c and secondly use Runtime.exec(String[]) api. So overall my method call now looks like String[] commands = new String[]{"/bin/sh","-c","grep -l executor *"}; exec(commands); Works just fine and result is displayed Commented Jan 2, 2014 at 7:33

1 Answer 1

0

The * is interpreted by the shell, not grep itself. So it doesn't work if you start grep from your Java program.

You can use grep with -r to make it search recursively, then use . for the current directory:

grep -lr executor .

To simulate the behaviour of the shell, just replace * with a space separated list of all files in the folder you want to search in. You can get the list of all files with:

StringBuilder str = new StringBuilder();

for (File f : new File("your folder").listFiles()) {
    if (f.isFile()) {
        str.append(' ').append(f.getPath());
    }
}

Now just replace the * with the result of str.toString().

Sign up to request clarification or add additional context in comments.

1 Comment

I guess you are bang on that * is interprestted by shell and thats why I am facing this problem. But to use grep . and recursivly is expensive and impacts on performance as it will include unwanted files to grep whic I would like to avoid. On other note, what would be the best way to tackle * in grep? any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.