0

I need to copy files of specific pattern from one director to another

File Pattern: "nm.cdr.*(asterisk)-2014-08-16-14*(asterisk).gz"

Command: "cp " + inputPath + filesPattern + " " + destPath;

If i use specific file instead of using * than it works fine(for single file) but with pattern using * it doesn't work.

Edit 1: I tried following code:

public void runtimeExec(String cmd)
{
    StringBuffer output = new StringBuffer();

        Process p;
        try
        {
            p = Runtime.getRuntime().exec(cmd);
            p.waitFor();
            BufferedReader reader = 
                new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";           
            while ((line = reader.readLine())!= null) {
                    output.append(line + "\n");
            }

        } 
        catch (IOException | InterruptedException e) 
        {
            LogProperties.log.error(e);
        }
}
1

2 Answers 2

3

The asterisk is something interpreted by the shell, so you need to use the shell as the main process, the command line for the process in Java would be something like bash -c '/origin/path/nm.cdr.*-2014-08-16-14*.gz /destination/path'.

Now, if you try to use this command in a single string it won't work, you need to use a String[] instead of a String. So you need to do the following:

1: change your method's signature to use a String[]:

public void runtimeExec(String[] cmd)

2: call your method with this value for cmd:

String[] cmd = new String[] {"bash", "-c",
    "cp " + imputPath + filesPattern + " " + destPath};
Sign up to request clarification or add additional context in comments.

Comments

2

Can't see what exactly is passed as a command, but on linux often is necessary to split command and parameters to string array, like:

String[] cmd = {"cp", inputPath+filesPattern, destPath};

1 Comment

cmd = cp /var/logs/nm.cdr.*-2014-08-16-14*.gz /var/logs2

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.