MacOS – How to rename an ordered list of files with an ordered list of file names

applescriptautomatorfindermacosterminal

I have a series of files. I need to apply a series of file names.

So,

  • 001.jpg -> apple.jpg
  • 002.jpg -> banana.jpg
  • 003.jpg -> orange.jpg
  • etc.

I will have to repeat this for each of 16 folders with 292 files per folder. The files will always be in the correct numbered order, but not always the same names (the first series will be 001.jpg-292.jpg, then 293.jpg-584.jpg, etc.). The supplied names will be the same for the files in all 16 folders.

I see that this is possible (and quite easy!) with apps like A Better Finder Rename, but I'm not keen on spending the $20 on all those features just to do one thing which I don't expect I'll ever have to do again 😀

So, is there some kind of free option, like Automator, an Applescript, or a shell script?

I've found several shell scripts that repeat the same action for all files, but I don't know enough to edit them and supply a new name for each rename operation. 🙁

Even if I have to make a script that's 292+ lines long, hard-coding the name list into it, that would be easier than manually renaming them all in finder one-by-one!

Best Answer

For example save this file as filenames.txt:

apple
banana
orange

Then run:

a=($(<filenames.txt));for d in *;do i=0;for f in $d/*;do echo mv $f $d/${a[i++]}.jpg;done;done

Remove echo if the output looks right.

If the file names contain spaces, add IFS=$'\n'; to the start and add double quotes around the variables.