Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Compact sparse disk images via the command line System
As you may know Sparse Disk Images grow in size as needed automatically when you add files to the image. However they don't shrink in size when you remove files. This limits the utility of them for many purposes, such as maintaining an adaptable sized HFS partion on a USB thumb drive. Well it turns out shrinking those bloated sparse images is not a problem. Just unmount the image if it is mounted. Then pull up a Terminal window and type:
hdiutil compact /some_path/your.sparseimage
where /some_path/your.sparseimage is the path and file name of the sparse disk image. This will harmlessly reduce the sparse image down to the minimal size needed to contain the files within it. It does not erase the disk image and you won't lose any files doing this. As a side effect, it is likely the files in the image and the imagefile itself will be defragmented in the process. The process is very fast if the image is mostly empty.

Of course it is a swell idea to make a backup copy of the sparseimage before you compact it, just in case!
    •    
  • Currently 3.36 / 5
  You rated: 5 / 5 (11 votes cast)
 
[49,315 views]  

Compact sparse disk images via the command line | 8 comments | Create New Account
Click here to return to the 'Compact sparse disk images via the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Compact sparse disk images via the command line
Authored by: Jwink3101 on Oct 05, '04 04:11:00PM

Great hint. Although i do not know if it is an apple thing or UNIX thing, i will apple would make it so it can compact itself.



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: batspray on Jun 13, '06 07:07:57AM

Hi,

I was looking for this tip, but I'm not sure I could make it work.

I followed the procedure and used the command line as described, but my .sparseimage file has not shrinked a bit!! It is still 95 GB on disk. However, when I mount it, the Finder reports that the disk is 145 GB in size, with about 13 GB free. I suspect that the .sparseimage is in compressed format: I used Intego's PersonalBackup to create it, and I can't remember if I used the compress option (it may also be mandatory, I don't recall).

Does this mean that one can't compact a .sparseimage file when it is in compressed form? If so, is there an alternate way of reclaiming the free space. I could very well make use of these free 13 GB since I'm currently editing a three-tracks DV video, of 1hr15 each!! Without duplicating the 95 GB file, of course (since I don't have enough space to do so).

Thanks in advance,
batspray



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: JYF on Mar 16, '07 01:35:42PM
Try this command in Terminal :

hdiutil resize -sectors min /SomePathToYourImage/YourImage.sparseimage

It worked for me. Practice on a backup image though.

[ Reply to This | # ]

Compact sparse disk images via the command line
Authored by: Analog on Nov 16, '08 05:37:40PM

In case it helps someone, I tried this on a 1.16TB image and it shrunk down to 984GB which does reflect the actual amount used. I did it in just about the worst conditions possible, only 82GB left on the 4.1TB volume and with a relatively slow cpu (1ghz G4.) Took about a day, but it worked!



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: Analog on Nov 16, '08 05:40:21PM

Oh yeah, and the image mentioned above is encrypted. I also tried this on a few GB file from a modern machine with no encryption and it was instant.



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: artistry on Jun 13, '09 12:31:07PM

Update - FreeDMG seems to be doing the trick as a 134Gb sparsebundle is already at 3.2Gb and counting. Either that or it's slowly deleting everything.

Oh well... Fingers crossed!



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: UberFu on Aug 15, '09 12:00:08PM

Worked Perfectly the first time.

Thanks for this.



[ Reply to This | # ]
Compact sparse disk images via the command line
Authored by: enthemic on Sep 23, '11 11:31:08AM

The will only work on unencrypted sparseimage files. If the sparse image file was created with a password hdiutil will error out reporting that it access is denied (because it expects a password to be entered via stdin and the workflow doesn't provide one.) Update the script as below to cause hdiutil to open up a GUI window to prompt for the password instead. This works with both encrypted and unencrypted spareimage files.

hdiutil compact "$@" </dev/null



[ Reply to This | # ]