Sometimes I need a big dummy file: to test a disk quota, to fill a partition on purpose or to see how a tool behaves with a 1 GB input. dd works, but it writes every byte. fallocate does not.
fallocate -l 1G test.img
ls -lh test.img
du -h test.img
The command asks the filesystem to reserve the blocks. On filesystems that support it, the allocation is quick because the blocks are marked as uninitialized and nothing is written. The size in ls and the usage in du should both show 1G, which tells you the space is really reserved, unlike a sparse file made with truncate.
Remove the file again when you are done:
rm test.img
Be careful which directory you run it in. A large reservation can fill a disk and make other things on that system fail.
The caveat is the filesystem. Not all of them support preallocation, and then the command fails with an error. A file made this way is also not always suitable as a swap file: reports exist of swapon refusing it with a message about holes, depending on the filesystem. For swap, follow the documentation of your distribution, which may tell you to use dd for the file or a filesystem-specific command instead.
Further reading: fallocate(1) manual page