How to use the command 'fallocate' (with examples)
- Linux
- December 25, 2023
The ‘fallocate’ command is used to reserve or deallocate disk space to files. Unlike other commands that allocate space by writing zeros to the file, ‘fallocate’ does not zero the blocks on the disk. It is a powerful tool that can be used to efficiently allocate or deallocate space on a file system.
Use case 1: Reserve a file taking up 700 MiB of disk space
Code:
fallocate --length 700M path/to/file
Motivation: The use case demonstrates how to reserve a specific amount of disk space for a file. This can be useful in situations where you want to ensure that enough space is allocated for a file before writing to it to avoid potential space allocation issues.
Explanation:
--length 700M
specifies the length of the space that needs to be allocated. In this case, it is 700 MiB.path/to/file
specifies the path to the file to which the space needs to be allocated.
Example output:
$ fallocate --length 700M path/to/file
This command will reserve 700 MiB of disk space for the file located at ‘path/to/file’.
Use case 2: Shrink an already allocated file by 200 MiB
Code:
fallocate --collapse-range --length 200M path/to/file
Motivation: This use case illustrates how to shrink the size of an already allocated file. This can be helpful in situations where you want to reclaim disk space by reducing the size of a file that is no longer needed.
Explanation:
--collapse-range
instructs ‘fallocate’ to collapse the specified range, which means reducing the file size.--length 200M
specifies the amount by which the file should be shrunk. In this case, it is 200 MiB.path/to/file
specifies the path to the file that needs to be shrunk.
Example Output:
$ fallocate --collapse-range --length 200M path/to/file
This command will shrink the size of the file located at ‘path/to/file’ by 200 MiB.
Use case 3: Shrink 20 MB of space after 100 MiB in a file
Code:
fallocate --collapse-range --offset 100M --length 20M path/to/file
Motivation: This use case demonstrates how to shrink a specific range of an already allocated file. It can be useful when you want to reclaim disk space by reducing the size of a specific section within a file.
Explanation:
--collapse-range
instructs ‘fallocate’ to collapse the specified range, which means reducing the file size.--offset 100M
specifies the offset from which the shrinking should start. In this case, it is 100 MiB.--length 20M
specifies the amount of space that needs to be shrunk. In this case, it is 20 MiB.path/to/file
specifies the path to the file that needs to be shrunk.
Example Output:
$ fallocate --collapse-range --offset 100M --length 20M path/to/file
This command will shrink 20 MiB of space after 100 MiB within the file located at ‘path/to/file’.
Conclusion:
The ‘fallocate’ command is a versatile tool for reserving or deallocating disk space to files. It provides options to allocate, shrink, or modify specific ranges within a file, allowing for efficient space management. By understanding the different use cases and arguments of ‘fallocate’, users can more effectively manage their file system’s disk space.