How to “zero” a file with 1's
Question
I'll often create a file of a particular size with a trick like
dd if=/dev/zero of=1gb.dd bs=1M count=1024
or perhaps even
dd if=/dev/urandom of=1gb.dd bs=1M count=1024
dd if=/dev/random of=1gb.dd bs=1M count=1024
But what if I want to get all 1's instead of 0's or random?
Solution
For random data, in almost all cases use /dev/urandom
. (You can also use /dev/random
, but that's much much slower because it's entropy-bound. urandom
is a PRNG which self-seeds from random
.)
For a non-zero file, I'd suggest something like this:
perl -e 'print chr(0xff) x 1000' > t
Obviously, customise the 0xff and 1000 to taste.
OTHER TIPS
This takes about four times as long as dd if=/dev/zero
, but only slightly longer than the Perl command in crazyscot's answer:
touch shred.out; yes $'\xff' | tr -d $'\n' | shred --random-source=/dev/stdin --size=1G --iterations=1 shred.out
Interestingly, this similar command stopped before it got to 1 GB:
yes $'\xff' | tr -d $'\n' | dd if=/dev/stdin of=1gb.dd bs=1M count=1024
Add another command on there after your dd.
sed 's/0/1/g' file.txt
It will replace all the 0's with 1's.
Probably a better solution out there, but that should work :-)
Edit: You'll actually have to pipe the result from that into either a new file or replace the same file. sed prints by default to the screen (IIRC)