OSX Find command and patterns

I’ve been working on learning to create Joomla modules and plugins, and needed to do some bulk renaming on files, folders and the contents of those files (some of which are .ini, some are .xml and lots are .php).

To rename the files, I ended up using Automator on the mac – this is a nice visualization tool to do bulk rename of folders and files.

For the file content, I used the following command:

find . -type f -name '*.xml' -exec sed -i '' s/OLDNAME/NEWNAME/ {} +

This finds all the .xml files in your current path, passes them to sed, which then replaces the text OLDNAME with NEWNAME.  Because I also had some case variations, I used three other commands:

find . -type f -name '*.xml' -exec sed -i '' s/OLDName/NEWName/ {} +
find . -type f -name '*.xml' -exec sed -i '' s/oldname/newname/ {} +
find . -type f -name '*.xml' -exec sed -i '' s/Old Name/New Name/ {} +

Combine this with all 3 file types and it’s a lot of typing.  A read of the manual file for find says that -name uses the standard ‘pattern’ system, and then after some googling, I discovered you can use [ ] to have it match one character from the list, so I split up the file types and used the following:

find . -type f -name '*.[xpi][mhn][lpi]' -exec sed -i '' s/Old Name/New Name/ {} +

That worked as expected, and I was able to hunt for all 3 extensions with one find command.

Now I’m sure someone out there can probably point out how to use regex to do it correctly, but hey, my method worked, and that’s all I cared about…

I just discovered, I’ve a fourth file type that needed to be updated… ‘.sql’.  So I interleaved that in my line and voila!

find . -type f -name '*.[xpis][mhnq][lpil]' -exec sed -i '' s/Old Name/New Name/ {} +

(I probably don’t need that last ‘l’ but for completeness I left it in…)

Leave a Reply