8

I have few thousand files to rename.

The thing is I have the old file names and new file names as columns in a spreadsheet. Without renaming them one by one. Is there a better way to do this?

Appreciate any help. I have tried the following with little satisfaction:

  • File Renamer - This allows me to rename the specified files from an external list in .txt. However the Trial version limits to 500 items. I do not wish to pay $20 just for this manual job.

    Could have installed them in separated computers or virtual machine though.

  • Automator - Get specified items > Rename >

    Not as effectively as I thought it would.

Just looking for some smarter solution. Appreciate any help

2
  • What version of OSX? Finder has a built in albeit limited method on 10.4 and above.
    – bjbk
    Commented Apr 26, 2016 at 13:06
  • OSX Version: El Capitan
    – Pete
    Commented Apr 26, 2016 at 13:28

3 Answers 3

11
  • export into a CSV (unicode UTF-8 or any other 8bit encoding, not UTF-16): two columns, separated by ; (assuming there is no ; in a file name)
  • open Terminal
  • change to folder containing the files
  • run perl -pi -e 's/\r\n/\n/;' LISTOFNAMES.csv to fix CRLF issues
  • run while IFS=\; read old new; do mv "$old" "$new"; done < LISTOFNAMES.csv

PS: This will overwrite existing files if the new name already exists.

PPS: Test this on a temporary folder with just a few files first (and make sure you have a recent backup)

7
  • I got what you mean, just put them into ONE column (separated by ;) I tried that, it works.
    – Pete
    Commented Apr 26, 2016 at 13:29
  • That is the CR/LF ending issue - was the file created on a Windows machine - look at dos2unix command
    – mmmmmm
    Commented Apr 26, 2016 at 13:30
  • Most spreadsheet applications save exported CSV files with window line endings with the last line not terminated. Even worst Excel on a Mac saves CSV files with old style mac carriage returns by default. Your CSV file would need to be sanatized by setting the line endings to new line and terminating the last line.
    – fd0
    Commented Apr 26, 2016 at 13:30
  • 2
    I got what you mean, just put them into ONE column (separated by ;) I tried that, it works! Thanks
    – Pete
    Commented Apr 26, 2016 at 13:33
  • 1
    Sets the separator for the read command to ; (actually it's IFS=\; read old new), so if the CSV would be old-name;new-name the old and new name would be read into old and new.
    – nohillside
    Commented Apr 26, 2016 at 13:39
5

I've found this to be one of the most simple ways:

  • In a spreadsheet column, concatenate: "cp oldfilename newfilename" to create a copy with the new name.
    • Use "mv oldfilename newfilename" to replace the old files rather than copy.
  • Copy this concatenated column to a text file and save as rename.sh in the folder with the files to be renamed.
  • Open terminal, navigate to the folder with the files to be renamed and run bash rename.sh
0
2

A couple years back I wrote a blog post on performing bulk file operations. The first step was to copy the list of files from search results into a spreadsheet. Since you already have the spreadsheet, you are very close to having a script that can do the renaming for you.

Let’s assume you have old names in column A and new names in column B.

Write the following into the first cell of column C:

mv

Write the following into the first cell of column D:

=concatenate("""",A1,"""")

Write the following into the first cell of column E:

=concatenate("""",B1,"""")

Instruct spreadsheet application to “Fill > Down” column C, D and E.

Then copy all data rows from columns C, D and E. Paste the copied text into a text editor. You should get row reading:

mv "old file name" "new file name"

This command will move/rename the file named “old file name” to “new file name”. Warpping the file names in double quotes allows for file names containing spaces.

If you are satisfied with the commands, copy-paste the complete text to the shell in Terminal.app

The procedure is admitedly less elegant that having a script process your spreadsheet or CSV. It however has the advantage of you getting a chance to double-check the commands before running them. When working from search results, you can easily get values from other columns (e.g. modification date, image dimensions, etc.) and work these into the new file names.

You must log in to answer this question.

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