Shell – Rename multiple files, sub directories, and insert characters at specific location on OSX

osxrenameshell

I have files and directories starting with "cateory-icon-" in many sub directories. I am trying to rename them recursively to "category-icons-". Please note that I added missing "g" to the category and "s" at the end of icons.

I did look at tons of other questions dealing with renaming multiple files and directories but could not make any of them work for me!

A little bit explanation about the answer would be greatly appreciated.

EDITED: Here is an example of my directory structure:

test/cateory-icon-archery/cateory-icon-archery.png
                          cateory-icon-archery@2x.png

test/cateory-icon-automotive/cateory-icon-automotive.png
                             cateory-icon-automotive@2x.png

Best Answer

Since you need to do this recursively, find is the best tool to use. To alter the name, you can use sh in the -exec call. Here is an example:

find /some/path -name 'cateory-icon' -exec sh -c 'mv "$1" "${1%/*}/category-icon-${1#cateory-icon-*}"' -- {} \;

The ${1#cateory-icon-*} expansion gets the filename after cateory-icon.

Related Question