Wulf's Webden

The Webden on WordPress

Batch Renaming on the OSX Command Line

Put your propellor beanies on: I’m about to get geeky!

One of the command line utilities that I find incredibly useful on the Linux command line is rename. You can change the name of a file or a whole batch of files by providing a simple pattern. For example, I might want to add a date stamp to a directory full of photos:

rename 's|.jpg|.20140825.jpg' *.jpg

As soon as you have more than a handful of files that need to be renamed according a regular schema, this is an invaluable tool. Unfortunately it appears that OS X uses rename for an entirely different purpose. However, a little bit of searching shows that also supports a similar regular expression tool in the form ${string/search/replace} so I can get the same result using a slightly more verbose method:

for i in *.jpg; do mv $i ${i/.jpg/.20140825.jpg}; done

It isn’t quite as concise because I also have to include a loop to run the operation on every file in the directory but it gets the same result and saves having to download and install one of the many third party utilities that can be found online to so the same thing (meaning that it takes me less time to get up and running on a vanilla system).

Comments are closed.