Useful UNIX tip – generate 512 bytes of random data with dd

[problem]

You want to generate 512 bytes of random data

[/problem]

[solution]

Use UNIX dd (disk to disk) command with /dev/random blocks for truly random data, whereas /dev/urandom just pumps out whatever is available.

[/solution]

[example]

You need to escape the control characters, or else it will trash your screen. cat -ve escapes these control characters for you.

dd bs=1 count=512 if=/dev/urandom | cat -ve

dd is a very powerful command and can be used to read files, data, tapes, even disks, etc – varying the number of blocks with count and block size with bs.

if is the input device and of can be used to designate an output device.

dd bs=1 count=512 if=/dev/urandom | openssl base64

Also it can convert to upper and lower case, with conv=ucase, etc.

If you have an extremely large file, it can open at an offset – via the skip option, for example to skip the first 1k of the file and read 100 bytes:

dd if=access_log.techieblogs bs=1 skip=1024 count=1000  - - [04/Jun/2006:00:13:10 +0800] "GET /mysqldemo/run_q.php?database=mysql HTTP/1.1" 200 1364

[/example]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *