Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[25,779 views]  

Compact sparse disk images via the command line | 7 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 whomever posted them. This site is not responsible for what they say.
Compact sparse disk images via the command line
Authored by: Jwink3101 on Tue, Oct 5 2004 at 4:11PM PDT
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 Tue, Jun 13 2006 at 7:07AM PDT
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 Fri, Mar 16 2007 at 1:35PM PDT
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 Sun, Nov 16 2008 at 5:37PM PST
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 Sun, Nov 16 2008 at 5:40PM PST
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 Sat, Jun 13 2009 at 12:31PM PDT
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 Sat, Aug 15 2009 at 12:00PM PDT
Worked Perfectly the first time.

Thanks for this.

[ Reply to This | # ]