bashfilerenamebatch-renamefilelist

Batch files rename using filename list


I need to rename over 1500 *.jpg files. I have text file with filenames list with 2 columns, tab separated:

Old filenames - new filenames

How i can check first 4 digits from first column and old filename and rename to another 4 digits from 2 column?

//sorry for my english


Solution

  • Use a loop as shown below:

    while read -r old new
    do
        arr=( ${old}_*.jpg )
        if (( ${#arr[@]} == 1 ))
        then
            mv "${arr[0]}" "$new.jpg"
        else
            echo "Error: Multiple files found for $old: ${arr[@]}"
        fi    
    done < file
    

    Note that there is a safety check to ensure that you don't have multiple files with the same prefix. For example, if you have 1305_1.jpg and 1305_2.jpg you can't rename them both to 1979.jpg, so the script will print an error.