12

What I have and want to achieve:

  1. There are thousands of images in one folder.

  2. I have a CSV file with following columns:

    A: original name B: renamed name

    A typical row looks like this:

    "original-1.jpg","renamed-1.jpg"  
    "original-2.jpg","renamed-2.jpg"
    

    I can remove the quotes, that's not a problem.

  3. Now I want to use an app or run a script that will search for all of the images in column A and rename them to names in column B (e. g. original-1.jpg -> renamed-1.jpg).

There are some answers around, e. g.:

http://ubuntuforums.org/showthread.php?t=1069652

http://systembash.com/content/one-line-batch-rename-files-using-csv-input-file-and-awk/

However, there's some scripting involved and I'm not sure if all of those scripts affect only the folder where you store the script or they can rename all of the files on the disk that satisfy certain conditions. Of course, latter needs to be avoided.

What I am looking for is a simple guide how to rename files and how to choose a folder with files.

Thank you in advance.

1 Answer 1

14

This should work for you:

sed 's/"//g' files.csv | while IFS=, read orig new; do mv "$orig" "$new"; done 

Explanation:

  • sed 's/"//g' files.csv : remove the quotes
  • IFS=, : split the input on ,
  • while read orig new; do ... done : This will read each input line, split it on the value of $IFS (here a comma) and save the 1st field as $orig and the rest as $new.
  • mv "$orig" "$new" : this will rename the files as requested.

If your file only contains file names (like orig.jpg) and no paths (not /home/take2/orig.jpg or similar), the command above will only affect files in your current directory. So, you need to open a terminal, cd to the target directory and run it there.

Test first:

To test this, you can do a dry run first by printing the commands that will be run without actually executing them:

sed 's/"//g' files.csv | while IFS=, read orig new; do echo mv "$orig" "$new"; done 
11
  • Great, this is explained better. :) Just to check before running it - I have to cd to the target directory and run this line or I need to save that line as example.sh and then run "example.sh" from terminal?
    – take2
    Mar 24, 2014 at 15:06
  • @take2 you can just run the line directly. I updated the answer with a way to see what will be run without actually changing your files, I recommend you do that first. Mar 24, 2014 at 15:08
  • Wonderful, it works! Just one more thing - does it care what are the labels of the columns (they are "orig" and "new" at the moment) or it just renames column A names to column B names?
    – take2
    Mar 24, 2014 at 15:11
  • @take2 what labels? No, it just takes the first comma separated field and renames it to the second. Note that if there are more than two fields, it will rename it to the rest of the line. As long as your input file is like what you posted it will work as expected. Mar 24, 2014 at 15:13
  • Btw. Terminal also displays "mv: cannot stat ‘orig’: No such file or directory", although it did rename the file.
    – take2
    Mar 24, 2014 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.