Thursday, 6 February 2014

Renaming multiple files

Have you got a folder full of files that you want to rename in the same way? If you are using Linux or Mac you can do it with one line of commands.


I had a folder of images named 1.png, 2.png etc. and I wanted to rename them all to Date_Event_1.png, Date_Event_2.png etc.

Open a command line - Terminal on your Mac and navigate to the folder containing the files you wish to rename and type the following command:

for filename in *.png; do mv "$filename" "Date_Event_$filename"; done;

And just in case you want to revert back:
for filename in *.png; do mv $filename `echo $filename | sed 's/\Date_Event_//'`; done;

Note the two different apostrophes in that last command.

2 comments:

  1. The sed command is key to this. The manual is here https://www.gnu.org/software/sed/manual/sed.html#Common-Commands

    ReplyDelete
  2. To find and replace one character with another, for example an underscore with a dash... for filename in *.jpg; do mv $filename `echo $filename | sed 's/_/-/'`; done;

    ReplyDelete