Saturday, April 19, 2008

Creating large empty files in Linux / UNIX

To create large empty files in Linux or UNIX:

# dd if=/dev/zero of=filename bs=1024 count=desired

Example to create a 1GB file:

dd if=/dev/zero of=file_1GB bs=1024 count=1000
dd if=/dev/zero of=file_1GB bs=4096 count=250
dd if=/dev/zero of=file_1GB bs=2048 count=500

Example to create a 2GB file:

dd if=/dev/zero of=file_2GB bs=2048 count=1000
dd if=/dev/zero of=file_2GB bs=1024 count=2000

Example to create a 512MB file:

dd if=/dev/zero of=file_512MB bs=1024 count=500
dd if=/dev/zero of=file_1GB bs=512 count=1000

either use

# mkfile size myfile

where can be in KB, MB or GB using k, m or g as suffix. To create a 10 GB file, use

# mkfile 10240m myfile

If you run # mkfile -n 10240m myfile the file will be created, but the disk blocks will not get allocated, only when writing data into the file.


Anonymous said...

I believe your count size is wrong. A 1GB file should have a count of 1024000 with a block size of 1024, i.e. bs=1024 count=1024000.

Naveen said...

or for easiness 'bs' can be specified in 'MB' i.e... bs=1M or what ever the requirement is, so for creating 1 GB file [dd if=/dev/zero of=testfile bs=1M count=1024]

Kenga Ru said...

Is there any way to create large file on SUSE Linux Enterprise Server 10 in the same way the Solaris utility mkfile do?

I need to just allocate space without actually writing anything to that file.

Anonymous said...

@Kenga the same procedure listed in the article should work for SUSE as well. Did you try the dd command?

Kenga Ru said...


"I need to just allocate space without actually writing anything to that file." This means that I don't want be smoking all the time 200Gb of zeros are beeing written to the disk by dd.

What if I need to make 10 500Gb chunks on my storage???? I should go smokin for an 24 hours?

Anonymous said...

@Kenga - I'm not aware of a method to do what you are asking, but I'm not saying it does not exist.

You could try to DD it once on the host, then use your storage system to snap the volume and clone it however many times over. Seems that would be faster likely.

Anonymous said...

If you don't want to wait an hour with dd, try this :

truncate -s M 10 output.file
It creates a 10 MB file... instantaneously

(M stands for 1024*1024 bytes, MB stands for 1000*1000)

Anonymous said...

i woul also like to point out ..
if you need to create large files inside of a virtual machine anon's

"truncate -s M 10 output.file
It creates a 10 MB file... instantaneously"
would be the best option primeraly due to the reduced Read/Write speeds that virtual machines "annoyingly" possess i create many 100s of these "large files" on daily basis when creating raw HD images within my virtual cloud . all running debian one of these VM's can create 30-40 50gb files in almost no time at all

Indie said...

You can create an empty file with dd by using the seek option with a count of 0.
E.g. to create a 1G file use
dd if=/dev/zero of=disk.img bs=1 count=0 seek=1G