3

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 :D

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!

2 Answers 2

2

For example save this file as filenames.txt:

apple
banana
orange

Then open Terminal and 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.

3
  • Thanks! This almost works. The only problem is, it uses the first name in filenames.txt for all 292 files! …any ideas?
    – iynque
    Commented Dec 20, 2013 at 1:34
  • Did you change the working directory to the folder that contains the other folders? Or if you need to run the script in each folder separately, remove the for d in *; part and ;done from the end.
    – Lri
    Commented Dec 22, 2013 at 12:48
  • I did edit the command and ended up with output like "mv 001.jpg apple.jpg; mv 002.jpg apple.jpg; mv 003.jpg apple.jpg…" etc.
    – iynque
    Commented Dec 23, 2013 at 15:08
-1

What I have done:

  • Paste the list of old names into the second column of a spreadsheet.
  • Paste the list of new names into the third column
  • verify the pair matching is correct
  • put mv into every cell of the first column
  • if any file name contains spaces, escape them with \
  • paste the entire contents of the spreadsheet into a script
  • if not using zsh, change all the tabs to space
  • chmod o+x on the script
  • run the script
1
  • Cool. Two answers, and the one that actually works gets a downvote.
    – WGroleau
    Commented Dec 5, 2022 at 8:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .