Recurse Center: Round Two!BlogJacob deGroot-Maggetti

A Quirk in the MacOS Filesystem

There’s a weird thing with files in MacOS: while you’re able to save a file with a filename that uses uppercase and lowercase characters, it seems that the filesystem treats uppercase and lowercase letters as equivalent:

$ ls                        # behold! there is nothing in this folder
$ touch File.txt            # let's make a file called File.txt
$ ls                        # there it is!
$ mv File.txt file.txt      # let's try to lowercase the filename
$ ls                        # now it's lowercase!
$ cp file.txt File.txt      # and yet the two filenames cannot coexist
cp: File.txt and file.txt are identical (not copied).
$ cp file.txt duplicate.txt # let's try duplicating the file...
$ ls                        # behold! there are two files in this folder
duplicate.txt file.txt
$ mv duplicate.txt File.txt # let's move the duplicate onto the old file
$ ls                        # behold! there is now one file in this folder
$                           # and for some reason, it's the lowercase version?!
$ echo "the contents of file.txt" > file.txt
$ echo "the contents of newfile.txt" > newfile.txt
$ ls                        # let's just make sure they're both there. good!
file.txt    newfile.txt
$ mv newfile.txt File.txt
$ ls
$ cat file.txt
the contents of newfile.txt
$ mv file.txt File.txt
$ echo "the contents of thirdfile.txt" > thirdfile.txt
$ ls
File.txt      thirdfile.txt
$ mv thirdfile.txt file.txt
$ ls
$ cat File.txt
the contents of thirdfile.txt

So it seems that in the MacOS terminal, when you try to create a new file with the same name as an existing file (case-insensitive), terminal treats the two files as the same. And when capitalization differs, terminal goes with the capitalization of the file that was there in the first place.

Finder, at least, makes it difficult to overwrite the contents of one file with another when filenames collide in this way:

A screenshot of Finder, showing a single file, file.txt
Here's file.txt
A screenshot of Finder, showing two files, file copy.txt and file.txt
Let's make a copy of it...
A screenshot of Finder, showing file copy.txt being renamed to File.txt
...and try to name the copy File.txt
A screenshot of Finder and an error message, reading "The name "File" with extension ".txt" is already taken. Please choose a different name."
It doesn't work!

This raises a few questions, most notably, what’s going on under the hood to allow filenames with different capitalizations to be displayed and persisted, even as something.txt and SoMeThInG.txt are, evidently in some fundamental way, equivalent?

It also has some ramifications for tools like Git, which is how I first came across this quirk. More on this soon!

Posted: Jun 21, 2024. Last updated: Jun 21, 2024.