How to tell fdisk to erase the partition's content like GParted does?

1

I'm trying to write a script to do some proceedings with an USB pendrive, deleting any partitions it encounters and creating new ones (more in this link).

When I do the proceedings using GParted, everything works fine, in particular when the new partitions are created with their file systems any old data is gone (and this operation doesn't take much time for the content I'm working with).

But when I do the equivalent proceedings with fdisk, I always find the old data in the same old partition as it was before!

How can I proceed such that the data of the old partitions is completely erased (and as fast as it happens with GParted)?

I tried commands dd if=/dev/zero of=/dev/sdb bs=512 count=1 and shred -vzn 0 /dev/sdb or similars but either they wouldn't actually erase the files, or they would take too much time to do it when compared to GParted.

Momergil

Posted 2015-12-03T12:33:28.183

Reputation: 511

2Off the top of my head, its fdisk, d for delete, w for write. You are writing, right? – Journeyman Geek – 2015-12-03T12:39:32.637

@JourneymanGeek that part is OK; it's just that the files continue to be there after such proceedings! – Momergil – 2015-12-03T12:42:21.127

I found something: when I try to use the call to mkfs.fat as done by GParted, the contents remain there, but if I follow this: http://unix.stackexchange.com/questions/40978/how-can-i-format-a-thumb-drive-so-that-i-delete-all-existing-files , then the contents are gone! The difference in the call is that the GParted call includes -v -l. I just can't say why for me this something-like-a-bug happens!

– Momergil – 2015-12-03T12:43:53.187

Answers

2

There is no such thing in fdisk. Unlike GParted, it does not concern itself with filesystems and partition contents at all – you need to do it manually.

Also, you didn't explain how you determined that "old data was gone" – did you examine the actual partition device, or did you just mount & ls it? A partition might be full of junk, and it does not matter, as long as the filesystem says "the root directory has 0 files in it".

I doubt GParted has a function to completely blank a partition – it's more likely that it simply created a new filesystem on top of whatever was there previously. (That said, on SSDs, the mkfs tools do discard the entire partition using TRIM, which is near-instant. However, with HDDs, nothing can make a disk spin faster – dd can't be much slower than GParted.)

So the following should be enough:

  1. Use wipefs --all /dev/xxx to erase the recognizable filesystem structures,
  2. Use mkfs.ext4 /dev/xxx (or mkfs.whatever-else) to create a blank filesystem.

user1686

Posted 2015-12-03T12:33:28.183

Reputation: 283 655