fallocate Command Examples in Linux

“fallocate” is a command-line tool in Linux-based systems that allows a user to reserve or deallocate disk space to files. This means that the user can allocate disk space to a file without actually writing data to the disk, or deallocate the disk space from a file without deleting the file itself.

The main advantage of using fallocate over other tools such as “dd” or “truncate” is that it can allocate or deallocate disk space without zeroing the data. This means that it does not need to write zeroes to the disk, which can be much faster than zeroing the data.

The fallocate command can be used to perform various operations on files such as:

  • Reserve disk space for a file: This will allocate disk space for a file without writing any data to it. This can be useful for creating large files that will be filled later, or for reserving space for a file that will be used as a swap file.
  • Deallocate disk space from a file: This will deallocate disk space from a file without deleting the file itself. This can be useful for shrinking a file to a smaller size, or for releasing space that is no longer needed.
  • Punch hole in a file: This will deallocate disk space from a file by creating a hole (a region of a file that contains no data) in it. This can be useful for reducing the physical size of a file, or for releasing space that is no longer needed.
  • Preallocate space for a file: This will allocate disk space for a file by writing zeroes to it. This can be useful for creating large files that will be filled later, or for reserving space for a file that will be used as a swap file.

fallocate Command Examples

1. Reserve a file taking up 700 MiB of disk space:

# fallocate --length 700M path/to/file

2. Shrink an already allocated file by 200 MiB:

# fallocate --collapse-range --length 200M path/to/file

3. Shrink 20 MB of space after 100 MiB in a file:

# fallocate --collapse-range --offset 100M --length 20M path/to/file
Related Post