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 _in.properties which is unrecognized by Java 17. To resolve this issue, I need to create copies of these resource bundle files that ends with _id.properties.

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

#!/bin/bash

# Set the root directory for the search
ROOT_DIR="/path/to/root_directory"

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

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

Explanation

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

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

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.