Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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