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!
File.txt
$ mv File.txt file.txt # let's try to lowercase the filename
$ ls # now it's lowercase!
file.txt
$ 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
file.txt
$ # 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
file.txt
$ 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
File.txt
$ 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](1-file-txt.png)
file.txt
![A screenshot of Finder, showing two files, file copy.txt and file.txt](2-file-copy-txt.png)
![A screenshot of Finder, showing file copy.txt being renamed to File.txt](3-uppercase-file-txt.png)
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."](4-already-taken.png)
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.