Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 983
Tom Avatar asked Jan 18 '14 02:01

Tom


People also ask

How do you Rename multiple folders on a Mac?

Rename multiple items On your Mac, select the items, then Control-click one of them. In the shortcut menu, choose Rename. In the pop-up menu below Rename Finder Items, choose to replace text in the names, add text to the names, or change the name format.

How do I bulk Rename a folder?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.


2 Answers

 find . -depth -name "*from_stuff*" -execdir sh -c 'mv {} $(echo {} | sed "s/from_stuff/to_stuff/")' \;
like image 181
lurker Avatar answered Oct 14 '22 11:10

lurker


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

like image 36
l'L'l Avatar answered Oct 14 '22 11:10

l'L'l