Making things match

Dealing with Case Inensitive File Extensions on Mac and Git

The Problem

Let’s say…hypothetically one was using a case insensitive operating system such as Mac OS X. Then let’s say one was deploying a website to a case-sensitive hosting service. To top it all off, you took some screen grabs and photos which had file names all in upper case, but for some reason you had named all the file extensions in lower case. As you might guess, this is exactly what just happened to me.

There’s two problems to solve here. One is that you need to rename all those files and the other is committing the rename from upper-case to lower-case into git. You could potentially do a find and replace in the HTML or Markdown code, but perhaps you have a convention that file extensions are all lower case or there is some hassle in changing your code. To solve the first issue, we just need to grab the rename command. If you are using homebrew then you just need the rename command:

brew install rename

(You can check to see if its already installed with brew info rename)

Once you have that command, its just a 2 step process to rename the files. Since the Mac sees FILE.JPG and FILE.jpg as the same file (the problem that got us into this mess in the first place), you’ll get errors if you just try to lowercase the files. Instead, we have to rename the files once to a different extension altogether, say .jpg1 and then to the lower-case extension we actually want .jpg

So first we run:

rename 's/\.JPG$/.jpg1/' *.JPG

The middle part is the regex to replace .JPG with .jpg1 and the wildcard at the end of the command *.JPG finds any files with the uppercase extension.

Next we just do the same command again except we remove the “1” we added to the extension:

rename 's/\.jpg1$/.jpg/' *.jpg1

After that, anything with a name like FILE.JPG will become FILE.jpg.

But what about Git?

So remember I said that there was a second issue? Git itself has to be aware of the changes and it won’t pick them up on a case-insensitive file system like the one on my Mac. So I have to tell git to be case SENSITIVE. Turns out, this is a simple configuration issue:

git config core.ignorecase false

Then you can do a git add . and git commit like usual. For me, I’m just going to leave git as case sensitive (ignorecase false) so that I don’t have to deal with this again.

programming software