Tom
Tom

Reputation: 9643

How to rename folders and files recursively in MacOS

I used to have a Debian machine and I remember using something like:

shopt -s globstar
rename 's/changethis/tothis/' **

But maybe because my bash version (version 3.2.48(1)) is not up to date I get:

-bash: shopt: globstar: invalid shell option name
-bash: rename: command not found

What would be different way to recursively rename files and folders in OS X? (10.8.5)


I want to rename every folder and file that have the string sunshine in it to sunset. so the file: post_thumbnails_sunshine will become post_thumbnails_sunset and r_sunshine-new will become r_sunset-new etc.

Upvotes: 12

Views: 5651

Answers (3)

l'L'l
l'L'l

Reputation: 47169

This should do the trick:

find . -name '*sunshine*' | while read f; do mv "$f" "${f//sunshine/sunset}"; done

*to specifically rename only files use -type f, for directories use -type d

Upvotes: 3

lurker
lurker

Reputation: 58244

 find . -depth -name "*from_stuff*" -execdir sh -c 'mv {} $(echo {} | sed "s/from_stuff/to_stuff/")' \;

Upvotes: 17

Sebastian
Sebastian

Reputation: 5865

You can use regular expression in the find command as stated @mbratch in the comments. You could use -regex, -iregex and -regextype to provide a complete expression to find. Then invoke -exec mv {} + (note the + sign).

Upvotes: 0

Related Questions