Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a partition, formatted ext4, that I want to empty completely. I will be repeating this write-then-empty process repeatedly, so I want the emptying step to be as quick as possible.

One option is to:

$ rm -rf *

But if the partition has lots of files and big ones, I've found it can take a significant amount of time.

Another option is to unmount the partition and reformat it, but I definitely prefer not taking this approach.

So, my question: Is there another way to empty a partition short of re-formatting it or traversing down the tree with rm -rf *?

share|improve this question

migrated from stackoverflow.com Sep 20 '12 at 3:52

This question came from our site for professional and enthusiast programmers.

    
Voting to move as not a programming quetion –  Adrian Cornish Aug 30 '12 at 1:45
add comment

1 Answer 1

To write zeros to a partition (e.g. /dev/sda2/) try the following

dd if=/dev/zero of=/dev/sda2 bs=1M

or write random data to partition

dd if=/dev/urandom of=/dev/sda2 bs=1M
share|improve this answer
    
You can double check if its empty with dd if=/dev/sda2 | hexdump -C | grep [^00] –  jfmbrennan Aug 30 '12 at 1:52
    
This would require unmounting the partition, right? Also, using dd would be much slower than just reformatting the partition. –  KP. Aug 30 '12 at 20:13
    
Yes it would. Another possible solution would be to use find . -type f -delete This is a faster and more robust approach to delete files without having to unmount a partition. –  jfmbrennan Aug 31 '12 at 1:04
    
How is "find . -type f -delete" different from "rm -rf *"? Would it be faster? –  KP. Sep 1 '12 at 0:26
    
I'm not exactly sure why it's faster but it is. I've read somewhere that its to do which buffer sizes but I don't quite understand it. If you want to run your own benchmarks, prefix your command with time –  jfmbrennan Sep 3 '12 at 0:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.