This really sounds like an XY problem.
sfill
, or shred
which is more generally available (part of coreutils) overwrite the contents of existing files (if everything goes well, e.g. the file system overwrites in place, and see other gotchas mentioned in their manuals).
If you wanted this, you could do so with shred -n 1 --random-source /dev/zero
; or a tiny shell script that gets the file's size and then does a dd conv=notrunc if=/dev/zero of=the_file_to_be_zeroed_out bs=... count=...
.
But as far as I understand, this is not what you need. (Unless can make sure that you only ever delete a file after zeroing it out, which sounds truly cumbersome and hardly feasible.) What you need is to zero the space that's currently unused by any file, so that instead of the remains of previously deleted files (which would still need to be compressed) the unused area is as compressible as possible, that is, preferably full of zeros.
You should create a new file that's as large as possible, and is full of zeros. Go with something like dd if=/dev/zero of=tmpfile bs=1M
, wait until it exits with the error message "No space left on device" and then delete this file. Your image is ready to be compressed – but don't forget to umount
it first!
dd
. It uses command-line utility calledshred
and has an option to set all bits to zero after the last iteration by adding the option-z
to theshred
command. – karel Mar 31 '19 at 18:10shred
program is provided by the coreutils package which is included with Ubuntu by default and is installed at/usr/bin/shred
– karel Mar 31 '19 at 18:38