Saturday, June 5, 2010

Hard Link Headslapper

Once again, I don't know where I have been lately (I'll resist the urge to say I was out in left field because in my younger days I was actually a good-field-no-hit center fielder), but I just recently discovered the virtues of hard links.

I have long wanted a way to allow my screen saver programs to access selected image files without actually copying those files to a separate folder, a process which would quickly gobble up disk space. For example, in baseball season I might want to include some baseball art, some player action photos, and some stadium scenes in my screen saver. I have folders containing photos of each category, but the photos are of mixed quality. I want to include only the better images. I currently face three unpleasant choices: display all of the images in a folder, including the inferior ones; display none of the images in a folder, thus excluding some outstanding ones; or selectively copy all of the desired files into a separate folder—and waste an inordinate amount disk space with duplicate versions of the same large image files.

I tried creating shortcuts to each specific image and consolidating those shortcuts into a single folder, but to no avail: none of the many screen saver programs I tried seem to recognize shortcuts. No matter: it turns out that hard links are the perfect solution. To make a long story short, use hard file links where you need multiple copies of a file but wish to save disk space by keeping only one physical copy on disk.

In Windows XP, create hard links using the following command line entry:
fsutil hardlink create   [NewFilename]   [ExistingFilename]
(Click the following link to view Microsoft's own documentation for creating hard links as well as some other uses for the fsutil command line utility.)

In Windows Vista and Windows 7, the equivalent command is:
mklink /H   [NewFilename]   [ExistingFilename]
I admit to being skeptical when I initially created my hard links. In Windows Explorer, the file sizes displayed in bytes for my new links duplicated those of the original files, and the total disk space reported for the folder containing my hard links showed a large number consistent with the sum of those individual figures. It first appeared that I was still consuming duplicate disk space just as if I had copied the files.

Only then I noticed the "disk free space" value. It had remained unchanged when I created my hard links. As a test, I deleted my newly-created links and instead copied the corresponding files to that same folder. The disk free space declined markedly. Next, I deleted the duplicate files, after which the disk free space increased again back to its original value. Finally, I re-established my hard links. Lo and behold: the disk free space remained the same. That experiment showed clearly that the hard links were in fact not consuming any extra disk space despite initial indications to the contrary.

Beyond that discovery, I found much to my delight that my screen saver program could now successfully read the hard file links where it previously had failed to read shortcuts. Similarly, my image viewer could fully read and edit the hard links in the same manner as if those hard links were the original images. Bingo! Exactly what I wanted.

This is one of those forehead-slapping moments where I find myself thinking, "If I had only known about this years ago!"

No comments:

Post a Comment