简体   繁体   中英

mv command for linux using java

I want to move all tar files from a directory to another directory using java code and my java code runs on linux machine.

I have tried below code but nothing happened -

try {
      String command = "mv " + "/home/" + name + "/*.tar"+ " "+ "/home/Program/MovedTar/"+ name + "/" + "";

      Process proc = Runtime.getRuntime().exec(command);
      int waitFor = proc.waitFor();

      closeStdStream(proc);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Below crossplatform solution for file handling.

Path source = Paths.get("/my/full/path");
Path target = Paths.get("/new/path")
try {
    Files.move(source, target,
    StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}

Here I have used java.nio.file . CopyOption is an interface from java.nio.file . The StandardCopyOption enum wich has 3 copy options:

  • ATOMIC_MOVE . Move the file as an atomic file system operation.
  • COPY_ATTRIBUTES . Copy attributes to the new file.
  • REPLACE_EXISTING . Replace an existing file if it exists.

First Get list of files in directory.

then use for loop and check for tar files.

and then use mv command to move that file.

You can use Apache common-exec library.

here is an example from the website

  CommandLine cmdLine = new CommandLine("AcroRd32.exe");
    cmdLine.addArgument("/p");
    cmdLine.addArgument("/h");
    cmdLine.addArgument("${file}");
    HashMap map = new HashMap();
    map.put("file", new File("invoice.pdf"));
    commandLine.setSubstitutionMap(map);

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
    Executor executor = new DefaultExecutor();
    executor.setExitValue(1);
    executor.setWatchdog(watchdog);
    executor.execute(cmdLine, resultHandler);

    // some time later the result handler callback was invoked so we
    // can safely request the exit value
    int exitValue = resultHandler.waitFor()

If you are using jdk-7 or upper version then you can use ProcessBuilder.Redirect -

commandString = "...";
ProcessBuilder pb = new ProcessBuilder(commandString);
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();  

ProcessBuilder has more option than Runtime.exec .

I found this link. I didn't try it.

https://docs.oracle.com/javase/tutorial/essential/io/move.html

Moving a File or Directory

You can move a file or directory by using the move(Path, Path, CopyOption...) method. The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.

Empty directories can be moved. If the directory is not empty, the move is allowed when the directory can be moved without moving the contents of that directory. On UNIX systems, moving a directory within the same partition generally consists of renaming the directory. In that situation, this method works even when the directory contains files.

This method takes a varargs argument – the following StandardCopyOption enums are supported:

REPLACE_EXISTING – Performs the move even when the target file already exists. If the target is a symbolic link, the symbolic link is replaced but what it points to is not affected.
ATOMIC_MOVE – Performs the move as an atomic file operation. If the file system does not support an atomic move, an exception is thrown. With an ATOMIC_MOVE you can move a file into a directory and be guaranteed that any process watching the directory accesses a complete file.

The following shows how to use the move method:

import static java.nio.file.StandardCopyOption.*;

...

Files.move(source, target, REPLACE_EXISTING);

Though you can implement the move method on a single directory as shown, the method is most often used with the file tree recursion mechanism. For more information, see Walking the File Tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM