A tale of two path separators
I was reminded yesterday that macOS will happily let you create files that appear to have slashes in their name, as shown here by the Finder:
I made the folder with the “New Folder” button in Finder, and the text file by saving it in TextEdit. Even though I understand how this works, it breaks my brain a bit every time.
If you didn’t know how this works, you could get a clue by running ls :
$ ls a:b:c/ x:y:z.txt
The files have either colons or slashes in their name, depending on how you look at them.
I don’t understand all the nuances, but I know this is a side-effect of the fact that macOS has not one but two path separators: the slash ( / ) and the colon ( : ). The two separators are used in different contexts, and the system will translate between them as needed.
These two separators reflect the two parent systems of modern macOS: classic Mac OS and the Unix-like NeXTSTEP. When they were joined together, Apple’s engineers had to build a file system that was compatible with both the classic Mac’s file system (the Mac OS Extended File System, aka HFS+), and with NeXTSTEP’s file system (the Unix file system, aka UFS). Among other differences, these systems had different path separators: HFS+ used a colon, while UFS used a slash.
There’s a Usenix 2000 paper written by several Apple engineers which describes the problems of combining classic Mac OS and Unix in more detail. It actually describes exactly what we’re seeing with the Finder and ls :
[The translation layer] can create a user-visible schizophrenia in the rare cases of file names containing colon characters, which appear to Carbon applications as slash characters, but to BSD programs and Cocoa applications as colons.
... continue reading