Generate File of a certain size?

  • I'd like to generate a file with the name example.file. I could use

    touch example.file

    but I want the file to be exactly 24MB in size. I already checked the manpage of touch, but there is no parameter like this. Is there an easy way to generate files of a certain size?

    We will all assume you do not care what the content is so filled with zeroes will be fine.

  • Paul92

    Paul92 Correct answer

    7 years ago

    You can use dd:

    dd if=/dev/zero of=output.dat  bs=24M  count=1


    dd if=/dev/zero of=output.dat  bs=1M  count=24

    or, on Mac,

    dd if=/dev/zero of=output.dat  bs=1m  count=24

    ... or use bs=1M and count=24. Many find it nicer and easier to read.

    Thank you for your observations. I edited the answer accordingly.

    Maybe dont use huge block sizes, my system did not like `bs=1G count=1`

    On the Mac, use 24m (small m), because the Mac doesn't like the big M. `dd if=/dev/zero of=output.dat bs=24m count=1`

    On Android (5.1.1, might (not) be version or phone specific) I had to use a lowercase `m` too.

    What is the problem with large block sizes specifically? I had hoped to use a 1G block size for a 1G file.

    Note that this doesn’t create a 24 MB file, but a 24 **MiB** one (=~ 25,2 MB) – for a 24 MB file use `bs=24MB` or `bs=1MB` respectively. I suppose it’s `mb` on a Mac.

    In my case, dd only created a file of 34MB. max. even if I used a bigger value in bs. Using bs=1M count=500 worked well.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM