8

I want to test an upload of files of content type application/x-gzip of various sizes ranging from 100 MB to 999 MB.

How can go I about creating .gz files of these predetermined sizes? If I do dd if=/dev/zero of=somefile bs=1 seek=100, the resultant file after zipping is usually very small.

19
  • You can create a 10MB gzip file like this:

    head -c 10M /dev/urandom | gzip -1 >10m.gz
    

    This uses urandom to get a high-entropy stream of bytes: since this is incompressible, the gzipped version will be about the same size as the input.

  • You can then catenate copies of your gzip file together:

    cat $(perl -e "print '10m.gz ' x 30") >300m.gz
    

    Thirty copies of the source file will be about 300MB, and 100 copies will be about a gigabyte.

3
2

Here is another method. You can create files by controlling the loop variable filecount. This creates random files of size between 100MB to 900 MB and gzips them.

#!/bin/bash
filecount=0
while [ $filecount -lt 10 ]
do
filesize=$((RANDOM%9+1))
filesize=$(($filesize*104857600))
</dev/urandom head -c "$filesize" | gzip > /tmp/file${filecount}.$RANDOM.gz
((filecount++))
done
2
  • 3
    The output from base64 is compressible by about 25%; I would recommend skipping the base64 bit and calling head directly on /dev/urandom.
    – dhag
    May 7 '15 at 19:25
  • 1
    @dhag thanks. I edited the answer to reflect that.
    – rahul
    May 7 '15 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.