tags:

views:

413

answers:

7

I'm trying to test some streaming file upload/download code that I've just written, but I need a huge (bigger than my memory, as in > 4GB) file to test on. What's a good way to quickly generate a file of a specific size (but no particular data inside)?

I know that I could just look around my hard drive for an appropriately-sized file, but I'm curious if there are any other good ways for generating test files, especially if I want a specific size. Downloading files is no good either, since my connection is slow.

+7  A: 

On Unix this is easy:

head -c <numbytes> /dev/random > bigfile.dat

So for 4 gigabytes this would be approximately:

head -c 4294967296 /dev/random > bigfile.dat
Sebastian Celis
for big files i would never use /dev/random but /dev/urandom instead - random has more entropy but will most probably block while generating it, while urandom will be a "good enough" source of randomness that doesn't block.
jcinacio
+1  A: 

On a *nix platform, you could do something like this:

dd if=/dev/zero of=test5000M.bin bs=5000000000 count=1

Which would generate a 5000mb file full of nothing.

On windows, something like this should work:

fsutil file createnew test5000M.bin 5000000000
Alex Fort
I just tried dd, and it doesn't accept such a large number for *bs*! It gives me this error: "dd: invalid number `5000000000'"
Daniel Lew
If the file transfer has any kind of compression, using a file of nulls might not represent typical usage, because it would compress too well.
Esko Luontola
A: 

I used to just do a seek and a write past the end of the file, to allocate huge chunks.

However, depending on the size you use, this can fail. (Near the 1.2 TB limit, in my experience on WinXP)

I used this method successfully for a hard-drive burn-in application for quite a few different system configurations.

John Gietzen
+4  A: 

On *nix systems, you could create a sparse file that hardly uses any real disk space.

dd if=/dev/zero bs=1 seek=4294967295 count=1 of=4G
ls -l 4G
-rw-r--r-- 1 me wheel 4294967296 2009-04-01 13:28 4G

This creates a file with a hole of size 4GB - 1 in the beginning. The hole does not consume filesystem blocks. You can even create sparse files that are too big to fit in your filesystem. Reading from the hole returns zeroes.

sigjuice
Excellent, this worked great. (Though I'd definitely use a different method if I'm trying to test anything involving compression algorithms.)
Daniel Lew
A: 

Here's a function to do it in Windows PowerShell:

function Create-TestFile([string]$location, [long]$sizeInBytes, [int]$numberOfFiles = 1 ,[char]$fillerChar="X")
{
$fc = new-object string ($fillerChar, $sizeInBytes ) ;
1..$numberOfFiles | %{ [io.file]::WriteAllText("$location\TestFile-$_.txt", $fc) }
}

And here's how you'd use it:

Create-TestFile "C:\temp" 2100 #creates a file 2100 bytes in size.
Eric Ness
+1  A: 

On Windows you can also use the fsutil command:

Usage : fsutil file createnew <filename> <length>
   Eg : fsutil file createnew C:\testfile.txt 1000

This will create a file called testfile.txt with size of 1,000 bytes. The file just contains NULs though, if that matters.

Patrick Cuff
A: 

I used to use an eatbits.exe utility back in the day. Where you could create these files. They are very valuable for installation testing.

I couldn't find eatbits anywhere online, it might have been an internal utility at the company I was working for.

ModelTester