I have a folder containing thousands of files:



I have a text file containing a list of new filenames I want to substitute:

old_filename     new_filename
file3.jpeg       my_dog.jpeg
file1.jpeg       my_cat.jpeg
file2.jpeg       my_goat.jpeg


I am looking for a script to rename all the files in the folder using the new filenames (or append the new name as a prefix to the original filename).

I think I need something that will search the folder, compare the name to the text file and then rename the file accordingly.

The lists and files are not sorted in any way.

  • So you're basically need something that prefixes mv to every line in your file that you can pipe to your shell? – Eugen Constantin Dinca Dec 22 '15 at 2:51
  • For example sed -n '2,$ p' <your_file> | sed 's/^/mv /' | bash – Eugen Constantin Dinca Dec 22 '15 at 2:57

First ensure you have a backup before trying this.

Your examples fortunately have no spaces in oldname and newnames, and no mention of sub-directories (all in same directory) so if all of the lines in your list look that way

  1. have rename_rules.txt in the same directory as your images
  2. cd into this directory
  3. run:

     eval "$(sed 's/^/mv /g' rename_rules.txt )"
    • where rename_rules.txt contain only pairs of oldname newname on each line
    • sed is used to convert your rename_rules into mv oldname newname
    • eval is then used to execute it, thus the file gets renamed

disclaimer: no validation or file checking is built into this simple short script at all, which is why I said the first thing to do is to be sure you have a backup in case one of your rename oldname newname pairs happens to contain a mistake, then you can at least start over from the backup

  • Thanks, giving it a try but get: mv: missing file operand Try mv --help' for more information. mv: missing file operand Try mv --help' for more information. – speld_rwong Dec 22 '15 at 4:05
  • I am newly familiar to sed - I use sed to convert html pages (that I download) into text files containing download links for the DownThemAll plugin in Firefox. I'm trying to figure out how to rename the files I download to something resembling that of the website the file came from - not just the useless gibberish download link from blogspot or somewhere else. – speld_rwong Dec 22 '15 at 4:13
  • Basically mv was not getting any input for one or more lines -- do you have empty lines in your rename_rules.txt ? – clarity123 Dec 22 '15 at 4:16
  • Just thought - there are two options I can live with: 1. rename or append to the filename of the file or 2. move the file into a new folder with a folder name that is specified in a text file as stated above - basically, search for a filename - if it exists in the search folder move the file into a new folder with a folder named specified in a text file. – speld_rwong Dec 22 '15 at 4:17
  • IT WORKED - MY MISTAKE - I WAS LOOKING IN MY BACKUP FOLDER - D'OH. - filenames in working directory were changed . – speld_rwong Dec 22 '15 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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