dd command bs option at end of disk
The whole output device is wiped whether its size is a multiple of the block size you pass to dd
or not.
The notrunc
flag has no effect when the output is a device file, because truncating a device file has no effect. If the output was a regular file, it would have the effect that the output file is not truncated before writing, which on some filesystems means that the old data is overwritten (as opposed to writing new blocks of data and leaving the rest unattached), however this wouldn't be useful since this property is not guaranteed by all filesystems and furthermore the command would not only overwrite the file, but also keep writing until it's filled the output disk (or some other error occurs).
Instead of using dd
and worrying about whether you're using it correctly (as it happens, it works in this particular case, but it's complicated and sometimes doesn't work), just use cat
.
cat /dev/zero >/dev/sdX
Despite popular belief on the web, there is absolutely no magic in dd
that makes it somehow better suited to writing to a disk. The magic is in the /dev
files. Any tool that can cope with binary data, such as any modern cat
or head
, can do the same job as dd
unless you're passing flags such as seek
or skip
.
Note that a problem shared by dd
and cat
is that on successful operation, they'll error out with “No space left on device” (ENOSPC). If you put this in a script, you'll either need to check that the error is ENOSPC or use a different method. A more reliable method is to first determine the size of the device (e.g. using /proc/partitions
under Linux), then write exactly the right number of bytes with a tool such as head
.
size=$(</proc/partitions awk '$4 == "sdX" {print $3}')
head -c "${size}k" /dev/zero >/dev/sdX
There is no problem — dd
writes to the very end (No space left on device
), unless it runs out of input, is limited by count=x
, or another error occurs (e.g. bad sectors, lousy cable etc.).
If overwriting an entire blockdevice is your goal, shred
is easier to use.
shred -v -n 1 /dev/deleteme # pseudorandom data
shred -v -n 0 -z /dev/deleteme # zeroes
Or cryptsetup
for random data with a verification step (zeroes are encrypted to random data; random data is decrypted back to zeroes).
Or blkdiscard
for SSDs.
Or wipefs
if it's just about starting over from scratch, not forcibly getting rid of everything you ever stored on a drive.