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.

terdon
  • 98,183
  • 15
  • 197
  • 293
take2
  • 337
  • 2
  • 3
  • 13

1 Answers1

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 
terdon
  • 98,183
  • 15
  • 197
  • 293
  • 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 '14 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. – terdon Mar 24 '14 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 '14 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. – terdon Mar 24 '14 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 '14 at 15:13
  • @take2 yes, that's probably the header (which you did not mention in your question). Since there is no file called `orig`, `mv` complains about not finding it. Don't worry about it, it will still continue with the rest of the commands. – terdon Mar 24 '14 at 15:14
  • First row in the CSV file - those are column labels (e. g. "Old names" and "New names"). Ok, got it. – take2 Mar 24 '14 at 15:14
  • Great, I'll just delete the header then. Thank you once more! – take2 Mar 24 '14 at 15:14
  • @terdon Why you first remove the quotes and then you put them again? What is wrong with: `while IFS=, read orig new; do mv $orig $new; done < file.csv`? – Radu Rădeanu Mar 24 '14 at 15:17
  • @RaduRădeanu : `mv: cannot stat ‘"original-1.jpg"’: No such file or directory`. The quotes will be treated as part of the string if they are read as part of the variable. I use them again _around_ the variable to protect from whitespace. – terdon Mar 24 '14 at 15:20
  • @terdon Ah, bravo. When I use "Test first" option (`...echo mv...`) the error has no way to appear ;) – Radu Rădeanu Mar 24 '14 at 15:26