Erase free disk space from the command line

Apr 24, '08 07:30:01AM

Contributed by: mubarak

It is possible to use Disk Utility to erase the free space on a disk. However, I had difficulty finding a command, such as hdiutil, to do it from the command line. One advantage of doing it from the command line is that it is easier to use cron or launchd to automate the process.

It turns out the solution is relatively straightforward:

cat /dev/zero > /private/tmp/junk; rm /private/tmp/junk
The first part of the command will keep enlarging a file with zeros until disk space runs out. After an error, the second part will delete the empty file. Lather, rinse, repeat for extra security (7-pass, 35-pass Guttman).

[robg adds: I haven't tested this one...however, I'd be somewhat cautious about using it on your boot drive. Running out of space on the boot drive -- even briefly -- may cause issues. If you know more about that possibility than I do, please comment!]

Comments (18)


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