Replace spaces in File and Folder names recursively in macOs Terminal
I’ve been working with a folder that has images in subdirectories and I needed to organize those and remove spaces from both directory and file names. And in this article I’ll just share that small piece of commands I’ve used to replace all the spaces from Directory and file names. We’ll do it in 2 steps.
1. Remove Spaces from Directory names recursively:
use the following code to replace spaces with underscores in all directory names recursively from current directory
find . -name "* *" -type d | rename 's/ /_/g'
2. Remove Spaces from File names recursively:
use the following code to replace spaces with underscores in all file names recursively from current directory
find . -name "* *" -type f | rename 's/ /_/g'
You may get an error saying invalid command rename, that’s because you probably don’t have rename installed. Do install rename, you can run the following command:
brew install rename
By the way, now you may get another error if you don’t have homebrew installed on your device. You can get information on installing homebrew from here
Credit for this solution actually goes to this stackoverflow answer , I had to make little changes to make it work with mac environment. That’s the reason I thought to share this on my blog as well.
I hope this small piece of commands will help you to get things done faster. Please let me know if it helped by leaving comments below. Have a good day.