TarasH
TarasH

Reputation: 55

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

Upvotes: 1

Views: 1543

Answers (3)

snnsnn
snnsnn

Reputation: 13610

I already posted this answer for another question but this one seemed more relatable in terms of the question, the keywords and the complexity, I will added it here too.

You can use small linux app name krename, with javascript plugin turned on. It is a free tool with very powerful renaming capabilities.

  1. Install Krename and open it
  2. Add files
  3. Go to plugins tab and add your javascript function to function definitions section, something like this:

    var files = [
        "Mickey",
        "Donald",
        "Duffy"
    ];
    
    function rename(){
      // krename_index is one of many special variables which can be added via ui
      return files[krename_index];
    }
    

This is a simple script that gets the job done, but it can be as complex as you like.

  1. Go to filename tab and call your function in the template input as shown below:

    [js; rename()]

You can prefix the above code with $ to keep the original file name and add to it. You can use "Functions" button to experiment further.

  1. Preview new names and complete renaming by clicking Finish button.

Upvotes: 1

dogbane
dogbane

Reputation: 274532

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.

Upvotes: 4

Raul Andres
Raul Andres

Reputation: 3806

Try this, and remove echo after verify it's that you want. changefile is the name of the file that contains changes

while read from to; do
   echo "mv ${from}* $to"
done < changefile

You should reverse order changefile to ensure that 1306 is applied BEFORE than 130 and BEFORE than 13

Upvotes: 2

Related Questions