fstrim is a command-line utility that is used to discard (or “trim”) unused blocks on a mounted file system. It is commonly used to reclaim space on solid-state drives (SSDs) and other types of flash-based storage devices. When a file is deleted on a file system, the space it occupied is not immediately reclaimed. Instead, the file system marks the space as available for reuse, but the data remains on the storage device until it is overwritten by new data. This can cause the storage device to become fragmented over time, resulting in reduced performance.
fstrim works by examining the file system to identify blocks that are no longer in use and sending a TRIM command to the storage device to discard them. This allows the storage device to reclaim the space and improves its performance by reducing fragmentation. The fstrim command can be used on a mounted file system, it can be scheduled to run on a regular basis using cron, systemd timer or other scheduling tools. The command can also be used with options to specify the file system path or the specific device that needs to be trimmed.
If you encounter the below error while running the command fstrim:
fstrim: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install util-linux |
Ubuntu | apt-get install util-linux |
Alpine | apk add util-linux |
Arch Linux | pacman -S util-linux |
Kali Linux | apt-get install util-linux |
CentOS | yum install util-linux |
Fedora | dnf install util-linux |
OS X | brew install util-linux |
Raspbian | apt-get install util-linux |
fstrim Command Examples
1. Trim unused blocks on all mounted partitions that support it:
# sudo fstrim --all
2. Trim unused blocks on a specified partition:
# sudo fstrim /
3. Display statistics after trimming:
# sudo fstrim --verbose /