I often find myself needing to rename batches of files. I find myself doing this when I have a set of images that are named
image-2x.png, and I duplicate them for 1x assests. I tried Automator to see if I could whip up a quick service that would remove the '-2x' from my 1x assests. I couldn't figure it out in 5 minutes, so I gave up (par for the course with these types of things). I then googled around a bit and turned up several tutorials using awk, sed, or some other custom function. None of that sounded very fun or easy; I then stumbled upon
Rename is a standard Unix utility that simply renames files given a pattern to search for and something (or nothing) to replace the pattern with. It supports standard things like globbing, and even supports testing your query to see the results! A basic command looks like this:
rename 's/2x copy//g' *.jpg Simple and perfect. In this case rename is going to strip out '2x copy' from any files that have that pattern and replace it with nothing, hence the double slashes at the end. That's all there is to it. Hopefully this helps someone else who just wants to rename a batch of files.
To get rename on a Mac, just brew it,
brew install rename.