Feed on

Defrag OS X files

Whats the problem?

You may think you don’t have to deal with disk defragmentation on OS X as OS X does it all for you. In most cases you are right, as OS X automatically tries to avoid fragmentation on files < 20MB. However, larger files are sometimes heavily affected. I noticed it when playing one of Valve’s Steam games like Portal or Half-Life 2. These titles use large game cache files (*.gcf) and everytime I started one of these games I heard my harddrive scratching which is not very common on an iMac.

How to find fragmented files?

The best way to find fragmented files on a Mac is a little command line tool called HFSdebug by Amit Singh. Just download and extract it. Open Terminal and type

cd Downloads/HFSDebug\ Lite\ 4.33/
sudo ./hfsdebug-lite -f -t 50

If you get a newer version your folder name may be different. This command starts hfsdebug, looks for fragmented  files ( -f ) and outputs the top 50 ( -t 50 ). If you want to see more or less files, just adjust the number. Now, lets look on our result:

# Top 50 Files with the Most Extents on the Volume
rank    extents   blk/extents       cnid path
1           923       5520.09    2546068 Macintosh HD:/Users/kai/Desktop/BDs/largefile1.iso
2           477      18376.86    2546070 Macintosh HD:/Users/kai/Desktop/BDs/largefile2.iso
3           238      16187.50    2546072 Macintosh HD:/Users/kai/Desktop/BDs/largefile3.iso
4           215        217.70    2733653 Macintosh HD:/Applications/Aperture.app/Contents/Resources/PointOfInterest.db
5           176        146.77    4008048 Macintosh HD:/Library/Application Support/VMware Fusion/isoimages/linux.iso
6           171        255.51    4059145 Macintosh HD:/Users/kai/Library/Caches/com.apple.Safari/Cache.db

Ever thought you have files on your disks that are fragmented in 1000 or even more parts? So is OS X stupid? It isn’t, look at the last sentence:

Out of 363749 non-zero data forks total, 361897 (99.491 %) have no fragmentation.
Out of 41259 non-zero resource forks total, 41257 (99.995 %) have no fragmentation.

Over 99% are not fragmented without ever using a defragmentation tool. Thats great!

Defragment files

The easy way to defragment your files is to buy a tool like iDefrag. For 25€ this will fix your 0.5% fragmentation “problem” 🙂 But as long as I don’t notice fragmentation I don’t care about it.

But as mentioned I don’t want to hear my harddisk when playing games so I decided to just defragment these game cache files. And this is simple: Just move the fragmented file to a different volume and move it back. OS X then looks for free continuous space on your drive and puts the file in it. To do this with Valve’s Steam games, type this into Terminal:

cp -Rpv ~/Library/Application\ Support/Steam /Volumes/YOURDRIVENAME
rm -rf ~/Library/Application\ Support/Steam
cp -Rpv /Volumes/YOURDRIVENAME/Steam ~/Library/Application\ Support/

The first command copies the Steam folder with the game cache file to my external, Mac formatted (thats important!) drive. The second one deletes the Steam folder on my local drive and the last one copies the Steam folder back to its old location.

Important: Don’t do step 2 and 3 when step 1 hasn’t succeeded! Always use a Mac formatted HFS+ volume to copy your data to! If you don’t have one, create a HFS+ formatted sparse image and mount it.

When finished, run hfsdebug again. In my case no Steam files were in the top 50 and I can’t hear the drive. Its also faster than before. Good luck!

3 Responses to “Defrag OS X files”

  1. Stefan says:

    Download does not work any longer

    Update from HSFdebug:
    “hfsdebug is obsolete and has been retired. Take a look at fileXray http://filexray.com/. You may also want to look at this comparison between fileXray and hfsdebug http://osxbook.com/blog/2011/04/17/filexray-vs-hfsdebug/

  2. Kai says:

    Thanks for pointing that out! You can use the free iDefrag demo to analyze your hard disk. Get it here: http://www.coriolis-systems.com/iDefrag.php

  3. tim Paul says:

    Yes, you are right that OS X’s HFS+ file system has the ability to defrag hard drive below files of size 20 MB. But if you want to defrag larger files you will use third party software( Stellar Drive Defrag). It is very easy to use and reliable tool.

Leave a Reply