I want to create an empty (may not be zeroed) file in ext4 file system. I can do this with:
dd if=/dev/zero of=file count=xxxx
But it takes a while if x is around 0.5T. I suppose there is a way to to create such file fast. It may contain gibberish it does not matter.
You could create a sparse file, but may not have the results you want depending on why you are doing this:
dd if=/dev/zero of=sparse_file bs=1 count=1 seek=512M
The problem is that if a program doesn't know how to handle sparse files, when do you something like copy the file, it will fill it with zeros. For example, see this previous serverfault post. You can see the difference with
du
as well:Lastly, if you do want to actually put in the zeros, if you set a higher bs it might be a little faster like bs=4096. Here is a link to someone who actually did some benchmarks with dd block sizes.
Would a sparse file suffice?
Use:
It is really fast if your filesystem supports sparse files.
File contents are zeroed, always. Linux won't allow you to reuse disk sectors without zeroing them since this is a security risk. For sparse files, sectors are only allocated when they are first written to.