0

Im using a mac.I have several folders containing png images.

I also have a list of their original file names(the images) and the names that i want to rename them to.

Can any one help me with a script for that ? (All methods that i have gone through are pattern matching based and they don't mention anything about renaming using a list)

i have the list as csv like this

"Crème Brulee Mica","E2DBCF"

the old name is "Crème Brûlée Mica.png" and the new name should be "E2DBCF.png".Like this i have hundreds of files

humblePilgrim
  • 1,818
  • 4
  • 25
  • 47

2 Answers2

0
sed -e "s/^\"\([^\"]*\)\",\"\([^\"]*\)\"$/mv '\1' '\2'/" | sh

... provided you do not have line breaks or single or double quotes in your file names.

Take out the | sh to test.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Spaces are fine, just not quotes (because they would need to be escaped) or newlines (because `sed` does not cope very well with data which is not purely line-oriented, without significant additional effort). – tripleee Jun 18 '14 at 09:20
0

Try it:

#!/bin/bash
renamed=0
failed=0
while IFS=',' read old_name new_name; do
    old_name=`echo ${old_name}.png | sed 's/\"//g'`
    new_name=`echo ${new_name}.png | sed 's/\"//g'`
    if [ -e "$new_name" ]; then
        mv "$new_name" "$old_name"
        #echo "$new_name renamed to $old_name"
        renamed=$[$renamed+1]
    else
        #echo "$new_name not found!"
        failed=$[$failed+1]
    fi
done < "myfile"
echo "success renamed: $renamed"
echo "failed rename: $failed"

Via IFS I add a delimiter that will save first value in old_name and second value in new_name variables. Then via sed command I replaced " to none value. then checking file existence if file exists then mv command replaces new_name to old_name. And at the end it will tell you count of success and failed actions.

Note: it works in current directory , except you add directory address in your files names.

mortymacs
  • 3,456
  • 3
  • 27
  • 53