Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've seen the following trick to create large, empty files somewhat quickly:

dd if=/dev/zero of=test.dat bs=1024 count=1024

However, this still takes some time when creating multi-GB files. I presume this time is dominated by the disk writing all the \0s from /dev/zero to test.dat.

Is there any way to "instantly" create a file of arbitrary size, without actually initializing its contents to anything?

share|improve this question
up vote 12 down vote accepted

Yes, if your filesystem supports sparse files.

Your dd method actually creates the files and fills the created file with zero's. It actually has to write those zeros and that takes time.

If you 'skip' ahead in the file without writing you will still create a seemingly large file, without actually writing to the file. Since this large file does not consume actually disk space it is even possible to create files larger than the FS it is on allows.

If you want to try this, use something like this
dd seek=123456 if=/dev/null of=sparse-file bs=1M

(Take care with backups and restores with software which is unaware of sparse files).

share|improve this answer
Thanks for the reference. Unfortunately, my Mac doesn't seem to support sparse files, and the file seems to still be filled with \0s. Is there any other alternative? – ChrisB Aug 7 '12 at 19:13
If the file is sparse and you read it you are supposed to get \0's. Note that my example used /dev/null and not /dev/zero. It did not write any zeros. – Hennes Aug 7 '12 at 19:23
Hmm, I should have read that wikipedia article more thoroughly. I missed the part where is mentions: " Most modern file systems support sparse files, including most Unix variants and NTFS, but notably not Apple's HFS+. " – Hennes Aug 7 '12 at 19:25
Sorry for the confusion -- my point is that the file creation time isn't any faster if I use the seek option to dd, it still takes several seconds per GB. Like you mention, Apple's filesystem doesn't seem to take advantage of the seek option – ChrisB Aug 7 '12 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .