fstrim Command Examples in Linux

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.

It is important to note that, fstrim command works only on file systems that support the TRIM command such as ext4, btrfs, f2fs and other modern file systems, it will not work on older file systems like ext3, NTFS, etc.

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 /
Related Post