Copy and rename file on different location

copy and rename file unix
copy and rename file linux
copy and rename file command line
linux copy and rename multiple files
batch file to copy and rename multiple files
copy and rename directory linux
copy and rename file linux command
copy directory and rename bash

I have one file example.tar.gz and I need to copy it to another location with different name example _test.tar.gz. I have tried with

private void copyFile(File srcFile, File destFile) throws IOException {

    InputStream oInStream = new FileInputStream(srcFile);
    OutputStream oOutStream = new FileOutputStream(destFile);

    // Transfer bytes from in to out
    byte[] oBytes = new byte[1024];
    int nLength;

    BufferedInputStream oBuffInputStream = new BufferedInputStream(oInStream);
    while((nLength = > 0) {
        oOutStream.write(oBytes, 0, nLength);


String from_path = new File("example.tar.gz");
File source = new File(from_path);

File destination = new File("/temp/example_test.tar.gz");

and then

copyFile(source, destination);

It doesn't work. The path is correct. It prints that the file exists. Can anybody help me?

Why to reinvent the wheel, just use FileUtils.copyFile(File srcFile, File destFile) , this will handle many scenarios for you

Copying, Moving and Renaming Files and Directories, -r -- recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location. -f -- force. Copies � In Windows Explorer, when you drag and drop a file from one location to another, you must let the copy (or move) operation complete before you can then rename the file in its new location. However, using the COPY command in the Windows Command Prompt, you can do it in one quick step.

I would suggest Apache commons FileUtils or NIO (direct OS calls)

or Just this

Credits to Josh - standard-concise-way-to-copy-a-file-in-java

File source=new File("example.tar.gz");
File destination=new File("/temp/example_test.tar.gz");



Changed to transferTo from @bestss

 public static void copyFile(File sourceFile, File destFile) throws IOException {
     if(!destFile.exists()) {

     FileChannel source = null;
     FileChannel destination = null;
     try {
      source = new RandomAccessFile(sourceFile,"rw").getChannel();
      destination = new RandomAccessFile(destFile,"rw").getChannel();

      long position = 0;
      long count    = source.size();

      source.transferTo(position, count, destination);
     finally {
      if(source != null) {
      if(destination != null) {

Copying and renaming files on Linux, You might need the same file in another location or you might want a copy because you're going to edit the file and want to be sure you have a� System.IO.Directory.CreateDirectory(targetPath); // To copy a file to another location and // overwrite the destination file if it already exists. System.IO.File.Copy(sourceFile, destFile, true); // To copy all the files in one directory to another directory. // Get the files in the source folder.

There is Files class in package java.nio.file. You can use the copy method.

Example: Files.copy(sourcePath, targetPath).

Create a targetPath object (which is an instance of Path) with the new name of your file.

Must copy and rename file, Copy and rename in the same time (also change filename, not only path): cp program3.cpp homework6.cpp. Rename only: mv program3.cpp� You then have a file-with-a-very-long-name and a file-with-a-very-long-name file-with-a-very-long-name-orig. Renaming files on Linux The traditional way to rename a file is to use the mv command.

How to copy and rename files in a different folder, How to copy and rename files in a different folder. Learn more about copyfile; movefile MATLAB. Renaming a file or folder would use the move command, since the copy command causes there to be duplicates. To rename a file, let the NEW LOCATION be a file with a different name. fso.MoveFolder "C:\Users\rock\Desktop\TestFolder\BigEyeCat.jpg", "C:\Users\rock\Desktop\TestFolder2\FunnyCat.jpg"

How can I copy a directory and rename it in the same command , However, if the target directory already exists, this would append the final part of the source if you don't want older files lying around there after the copy. The difference between /some/dir and /some/dir/. was discussed a while back in cp� After you’re comfortable with moving around the hierarchy of your hard drive in UNIX, it’s a cinch to copy, move, and rename files and folders. To copy files from the command line, use the cp command. Because using the cp command will copy a file from one place to another, it requires two operands: first the source and then the destination.

Create, Copy, Rename, and Remove Unix Files and Directories, Create, Copy, Rename, and Remove Unix Files and Directories Directories may contain files and/or other directories. Directories are analogous to File and directory names may be up to 256 characters long. Names may� TIP: In SSIS, Please use the Foreach Container to Copy multiple files from one location to another location. We have File System Task Folder inside the MSBI Folder. Our task is to Copy the cache.xml file inside the File System Task Folder to Copied Folder inside F Drive.

  • try flush() your streams before close() ing it.
  • Correct this code in your post: String from_path=new File("example.tar.gz");
  • @Mohamed, flush is never needed before close
  • you don't need createNewFile w/ FileOutputStream, also you should not use BufferedInputStream(), it doesn't really help. Just use larger than 1k byte[] oBytes. Last but not least, FileChannel.transferTo is the best way to copyStuff
  • Using FileStreams might be inefficient to copy files, look at java.nio.channels.FileChannel.transferTo