-1

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
  • 89,133
  • 25
  • 207
  • 233
  • 1
    Will it be faster? Maybe. You still have to read the bit then determine if its 1, then choose to write a 0 over it. Its probably not worth the effort. – Keltari May 21 '22 at 23:26
  • Solved on U&L SE: [*How to fill a device with zeros, without overwriting the bytes that are already zeros?*](https://unix.stackexchange.com/q/469399/108618) – Kamil Maciorowski May 21 '22 at 23:39
  • @KamilMaciorowski Oups, sorry. Didn't search enough. Will change the title as "solved". Thank you a lot. – Grenouille May 22 '22 at 00:07
  • 1
    @Grenouille that is not how these sites work. We have a tick mark next to answers that shows the problem as solved. The title and question body is entirely the wrong place to show you found a solution. It might help to take the [tour]. – Mokubai May 22 '22 at 04:16
  • I posted a minimal answer pointing to my answer on U&L SE. You can accept it if you wish. This way the question will be properly marked as solved. – Kamil Maciorowski May 23 '22 at 17:54
  • @Mokubai Sorry a lot. I took a look at the tour. I feel ashamed because usually I first read the website rules and then post but this time I did not. Oups. – Grenouille May 24 '22 at 18:41

1 Answers1

0

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.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • I did that, it did the job. In the future I may try to compare it to other methods. This methods however needs two pass: one reading, one -eventually- writing. I have no idea related to the technical aspects behind but I'm wondering if reading and eventually writing at the same time could be possible? Logically that would change nothing, but... Curious... – Grenouille May 24 '22 at 18:42
  • Sorry, I'm a beginner, I'm not allowed to vote on the other forum but anyway your answer answers my question. – Grenouille May 24 '22 at 18:51