A script to shrink and compress disk images easily

Mar 10, '03 09:30:00AM

Contributed by: carsten

Here is a tcsh shell command which will shrink the unused space of a disk image, then compress it with zlib-level 9.

Features:

Caveats: [robg adds: I haven't tested this one myself.]

Instructions:

  1. Add the following lines to ~/Library/init/tcsh/aliases.mine (or create the file if not present):
    
    alias dmgcon 'hdiutil convert -format UDRW \!:1 -o \!:1.UDRW \
     && hdiutil resize -sectors `hdiutil resize \!:1.UDRW.dmg | awk \{print\ \$1\}` \!:1.UDRW.dmg \
     && hdiutil convert -imagekey zlib-level=9 -format UDZO \!:1.UDRW.dmg -o \!:1.UDZO \
     && rm \!:1.UDRW.dmg \
     && mv \!:1 \!:1.bak \
     && mv \!:1.UDZO.dmg \!:1 ;\
     [ -f /Developer/Tools/SetFile ] && /Developer/Tools/SetFile -t devr -c ddsk \!:1 ;\
     [ -f /Developer/Tools/SetFile ] && /Developer/Tools/SetFile -t devr -c ddsk \!:1.bak ;\
     ls -alF \!:1.bak \!:1'
    
    Note: The punctuation and spacing are critical! Also notice that the commands do span multiple lines, this is okay because a "\" is used at the end of each line.

  2. Add the following lines to ~/Library/init/tcsh/completions.mine (or create the file if not present):
    
    complete dmgcon 'p/*/f:*.{dmg,img}/'
    
  3. Open a new terminal window for the changes to become active.

Try it out: "cd" to a directory containing a disk image, then type...


dmgcon [tab]
...to see an autocompleted list of .dmg or .img files. Finish the filename, hit return and watch it go.

Enjoy! You can also search for other hdiutil hints at Mac OS X Hints.

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030306095219454