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: , , , ,
This entry was posted on Sunday, February 28th, 2010 at 6:17 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “How to create a file of arbitrary size with shell script commands”

  1. How to create a file of arbitrary size with fsutil in Windows XP « Onkar Joshi's blog

    [...] How to create a file of arbitrary size with shell script commands 28 Feb [...]

  2. Erik

    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.

  3. Onkar Joshi

    @Erik
    You are right. Thanks for the note!

Leave a Reply

Your comment