How to create a file of arbitrary size with shell script commands
A few days ago I was working on writing Java code to transfer files via SFTP and FTPS. As part of the test cases, I wanted to try and transfer files of small medium and large sizes like 0 byte, 1 byte, 100 bytes, 1000 bytes, 1MB, 10MB. How would one go about creating files of a certain size in a Linux/Solarix/Unix environment?
You can do it with the dd command. You can use /dev/random or /dev/zero as a data source for random bytes or null bytes.
bs = block size
count = number of blocks
if = input file
of = output file
The total size of the file created will be bs x count bytes.
dd if=/dev/random of=myFile.dat bs=1024 count=102400
dd if=/dev/random of=myFile.dat bs=1024 count=1024
dd if=/dev/random of=myFile.dat bs=100 count=1
dd if=/dev/random of=myFile.dat bs=1 count=1
The above commands will create files of sizes 100MB, 1MB, 100 bytes, 1 byte respectively with random data.
Tags: Linux, script, shell, Solaris, Unix

[...] How to create a file of arbitrary size with shell script commands 28 Feb [...]
You may want to use /dev/urandom instead of /dev/random, since many machines will not have enough randomness to allow /dev/random to return enough data. This results in reads from /dev/random blocking, and eventually dd will timeout and leave you with less data in your output file than you would expect.
@Erik
You are right. Thanks for the note!