tags:

views:

187

answers:

3

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?

A: 

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)

Paul
I wonder how long sed would take on 1Gb file.
DVK
This is wrong. /dev/zero has all bits zero (0x00). Your sed command is looking for the ASCII 0 digit (0x30).
Matthew Flaschen
+2  A: 

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.

crazyscot
`python -c "print chr(0x31)*1000" > t` Samething only python flavored.
zdav
+1  A: 

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
Dennis Williamson
+1 for inventiveness :]
DRL