How to copy files ending with a specific string to another file in Linux/macOS?

In Java 17, the Locale for Indonesia changed from in_ID to id_ID. In my project, the resource bundle files are named with the suffix which is unrecognized by Java 17. To resolve this issue, I need to create copies of these resource bundle files that ends with

Here are solutions for both Linux/macOS (using Bash).


# Set the root directory for the search

# Find all files ending with '' and process each one
find "$ROOT_DIR" -type f -name "*" | while read -r FILE; do
    # Construct the new file name by replacing '' with ''
    # Copy the original file to the new file
    cp "$FILE" "$NEW_FILE"

Save this script as, make it executable with chmod +x, and run it with ./


  • find "$ROOT_DIR" -type f -name "*": Finds all files ending with
  • while read -r FILE; do ... done: Loops through each found file.
  • ${} Constructs the new file name by replacing with
  • cp "$FILE" "$NEW_FILE": Copies the original file to the new file.

These scripts will recursively search the specified directory for files ending with, then create a copy of each file with in the same directory.

How do I copy a file in JDK 7?

In this example you’ll see how to copy a file using the new API provided in the JDK 7. The first step is to define the source and the target of the file to be copied. For this we can use the Path class. To create an instance of Path we use the Paths.get() method by passing the path information as the arguments.

Next we can configure the file copy operation option. For this we can define it as an array of CopyOtion. We can use copy option such as StandardCopyOption.REPLACE_EXISTING and StandardCopyOption.COPY_ATTRIBUTES.

Finally, to copy the file we use the Files.copy() method. We give three arguments to this method, they are the source file, the target file and the copy options information.

Let’s see the code snippet below:


import java.nio.file.*;

public class NioFileCopyDemo {
    public static void main(String[] args) {
        // Define the source and target of the file to be copied.
        Path source = Paths.get("D:/resources/data.txt");
        Path target = Paths.get("D:/resources/data.bak");

        // Define the options used in the file copy process.
        CopyOption[] options = new CopyOption[]{

        try {
            // Copy file from source to target using the defined 
            // configuration.
            Files.copy(source, target, options);
        } catch (IOException e) {

How do I use Java NIO to copy file?

The following code snippet show you how to copy a file using the NIO API. The NIO (New IO) API is in the java.nio.* package. It requires at least Java 1.4 because the API was first included in this version. The JAVA NIO is a block based IO processing, instead of a stream based IO which is the old version IO processing in Java.


import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;

public class CopyFileExample {
    public static void main(String[] args) throws Exception {
        String source = "medical-report.txt";
        String destination = "medical-report-final.txt";

        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(destination);

        FileChannel inputChannel = fis.getChannel();
        FileChannel outputChannel = fos.getChannel();

        // Create a buffer with 1024 size for buffering data
        // while copying from source file to destination file.
        // To create the buffer here we used a static method
        // ByteBuffer.allocate()
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        // Here we start to read the source file and write it
        // to the destination file. We repeat this process
        // until the read method of input stream channel return
        // nothing (-1).
        while (true) {
            // Read a block of data and put it in the buffer
            int read =;

            // Did we reach the end of the channel? if yes
            // jump out the while-loop
            if (read == -1) {

            // flip the buffer

            // write to the destination channel and clear the buffer.

How do I copy some items of an array into another array?

In the code snippet below we are going to learn how to use the System.arraycopy() method to copy array values. This method will copy array from the specified source array, starting from the element specified by starting position. The array values will be copied to the target array, placed at the specified start position in the target array and will copy values for the specified number of elements.

The code snippet below will copy 3 elements from the letters array and place it in the results array. The copy start from the third elements which is the letter U and will be place at the beginning of the target array.

package org.kodejava.lang;

public class ArrayCopy {
    public static void main(String[] args) {
        // Creates letters array with 5 chars inside it.
        String[] letters = {"A", "I", "U", "E", "O"};

        // Create an array to where we are going to copy
        // some elements of the previous array.
        String[] results = new String[3];

        // Copy 3 characters from letters starting from the
        // third element and put it inside result array
        // beginning at the first element
        System.arraycopy(letters, 2, results, 0, 3);

        // Just print out what were got copied, it will
        // contains U, E, and O
        for (String result : results) {
            System.out.println("result = " + result);

The output of the code snippet:

result = U
result = E
result = O