Skip to content

fallocate

The Linux fallocate command is a utility used to preallocate space for a file. The command is an alternative to creating and filling a file with zeros, as it allows users to quickly allocate space for a file without writing any data to the disk.


Reserve a file taking up 1700 MiB of disk space

fallocate --length 1700M filename

Shrink an already allocated file by 100 MiB

fallocate --collapse-range --length 100M filename

Shrink 20 MB of space after 100 MiB in a file

fallocate --collapse-range --offset 100M --length 20M filename

Deallocate Space From File

Use the -d (--dig-hole) option to deallocate space from a file. This option creates a hole in the file, which frees up disk space.

fallocate -d -o 100M --length 200M filename

Create ext4 filesystem on the truncated file

mkfs.ext4 filename

Increasing swap

# create swap file
$ fallocate -l 1G /swapfile

# permissions set to root
$ chmod 600 /swapfile

# setup a linux swap area
$ mkswap /swapfile

# activate swap
$ swapon /swapfile

# verify
$ swapon --show

Create an encrypted preallocate filesystem for hide your private files inside

fallocate -l 1000MB my_big_file
dd if=/dev/zero of=my_big_file bs=1M count=10240
mkfs.ext4 my_big_file
cryptsetup luksFormat my_big_file