Renaming Files in Bulk Using the Command Line: A Step-by-Step Guide

Blog

Posted by Nuno Marques on 19 Nov 2024

Renaming multiple files manually can be time-consuming, especially when dealing with a large set of files. Fortunately, the command line (CLI) provides powerful tools to automate this process. In this post, I’ll show you how I renamed a batch of files with specific names (like Country=portugal.svg) to simplified two-letter codes (PT.svg) using a bash script.

The Problem

Imagine you have a folder full of files named like this:

Country=portugal.svg
Country=france.svg
Country=germany.svg

You need to rename them to:

PT.svg
FR.svg
DE.svg

To do this, you need:

  1. A mapping of country names to their respective two-letter codes.
  2. A script to rename the files.

The Solution

We’ll write a bash script to handle the renaming automatically.

Step 1: Prepare the Mapping File

Create a file called country_code_map.txt with your mappings:

afghanistan=AF
albania=AL
algeria=DZ
andorra=AD
angola=AO
antigua-and-barbuda=AG
argentina=AR
armenia=AM
australia=AU
portugal=PT
france=FR
germany=DE

Step 2: Write the Bash Script

Create a file called rename_flags.sh and paste the following script:

#!/bin/bash

# Path to the folder containing the flag files
FOLDER="./"

# Path to the mapping file
MAPPING_FILE="country_code_map.txt"

# Check if the mapping file exists
if [ ! -f "$MAPPING_FILE" ]; then
    echo "Error: Mapping file '$MAPPING_FILE' not found."
    exit 1
fi

# Rename files based on the mapping
for file in "$FOLDER"/Country=*.svg; do
    [ -e "$file" ] || continue  # Skip if no files match

    # Extract the country name from the filename
    base_name=$(basename "$file" .svg)
    country_name=${base_name#Country=} # Remove "Country=" prefix

    # Convert to lowercase for comparison (to match the mapping)
    country_name_lower=$(echo "$country_name" | tr '[:upper:]' '[:lower:]')

    # Find the corresponding country code in the mapping file
    country_code=$(grep "^$country_name_lower=" "$MAPPING_FILE" | cut -d'=' -f2)

    if [ -n "$country_code" ]; then
        # Rename the file to the two-letter code
        mv "$file" "$FOLDER/$country_code.svg"
        echo "Renamed $file to $FOLDER/$country_code.svg"
    else
        echo "No mapping found for $country_name"
    fi
done

Step 3: Make the Script Executable

Run the following command to make your script executable:

chmod +x rename_flags.sh

Step 4: Run the Script

Navigate to the folder containing your files and run the script:

bash rename_flags.sh

The script will rename all files starting with Country= to their respective two-letter codes.


Key Takeaways

  • Automate repetitive tasks: Scripts save time and reduce human error.
  • Use mappings: A separate mapping file makes your script reusable and easy to update.
  • Be cautious: Always test scripts in a safe environment to avoid unwanted changes.

With this approach, renaming hundreds of files becomes a breeze. If you have questions or ideas for improvements, please let me know. Thank you!