Understanding Hard Links

First signs of a problem

I copied my Movies directory from my old Mac to my new Mac, using target disk mode. On my old Mac, the Movies directory took up 84G. But on my new Mac, it took up 149G. What was going on?

My Movies directory contained hard links, which I wrote about last time.

The Movies directory contained 65G of files that were hard-linked to other files also within the Movies directory. When I copied them the usual way (by drag and drop, or cp), the hard linked files were copied one time for each hard link. So tons of duplication, tons of wasted space.

Investigating hard links

The command du, which calculates disk usage of a directory, is useful for understanding what hard links you have and where.

Continue reading “Understanding Hard Links”

Hard Links on Mac OS X in Movies and Pictures

Mac OS X Background

Early versions of Mac OS X did not have hard links, but Apple added them in Leopard 10.5 to make use of in Time Machine.  Initially, the functionality was crippled, but as of Mavericks 10.9 (and perhaps earlier), full hard link functionality is available.

What is a hard link?

In the file system, a filename points to a particular inode, which contains the metadata about the file:

file1 ---> [Inode 317905] ---> [Address, Ref Count = 1]

When you a create a hard link, two separate file names point to the same inode:

file1 ---
        |---> [Inode 317905] ---> [Address, Ref count = 2]
file2 ---

file1 and file2 are completely symmetric. If you delete file1, file2 will still exist and still point to inode 317905. And if you delete file2, file1 will still exist and still point to inode 317905.

The file system maintains a reference count within each inode. If you delete both file1 and file2, the reference count for the inode drops to 0, and the space used by the file is marked for deletion.

Continue reading “Hard Links on Mac OS X in Movies and Pictures”