0

I usually use

# pv -terab /dev/zero > /dev/sdx

to zero a disk (ie: /dev/sdx). Basically, it just writes zero everywhere without caring of the previous values.

However, I'm wondering if it wouldn't be faster, especially on USB flash drive, to read the disk and only IF the read value is different from zero, then set it to zero. How can I do that? Especially in a bash single command line.

The interest I get on that is that USB flash drive have several specificities. They are read quite quickly but written quite slowly. They tends to wear for too much writing. Last point, as far as I know, it is not possible to use efficiently the command "trim" on them.

Therefore writing only on the data of the disk that are not already set to zero should be worthy...

Thank you for answers.

PS: sorry for bad english, not my native language

Mokubai
  • 95,412

1 Answers1

1

I (the original author of this community wiki answer) solved this exact problem on Unix&Linux SE: How to fill a device with zeros, without overwriting the bytes that are already zeros?

The solution (ab)uses the --generate-mode option of GNU ddrescue. The core is:

ddrescue -b 512 --generate-mode /dev/zero /dev/FLASH flash.map
ddrescue -b 512 --force --fill-mode=+ /dev/zero /dev/FLASH flash.map

See the linked answer for details. If you find the solution helpful, please upvote the answer on Unix&Linux SE in the first place.


Disclaimer regarding "link-only answer":

I decided not to duplicate nor cite the entire other answer here. Since U&L SE is our sister site and my answer there is as much under my control as a non-wiki answer here would be, the above link will ever break only if the entire Stack Exchange breaks. I'd like to maintain one answer, not two. I hope the Community will understand.

On the other hand I felt some answer here was needed. Now it may be accepted and the question formally marked as solved. This is the point of this answer.