fstrim Command (with examples)
- Linux
- November 5, 2023
The fstrim
command is used to discard unused blocks on mounted filesystems, particularly on flash memory devices such as SSDs and microSD cards. It helps to free up space and maintain optimal performance on these storage devices. In this article, we will explore different use cases of the fstrim
command with code examples.
Use Case 1: Trim unused blocks on all mounted partitions that support it
sudo fstrim --all
Motivation:
When dealing with multiple partitions on a system, it can be time-consuming to individually specify each partition for trimming. Using the --all
option with fstrim
allows us to trim all mounted partitions that support trimming simultaneously, saving time and effort.
Explanation:
The --all
option indicates that we want to trim all mounted partitions. The command will identify and trim the unused blocks on each supported partition automatically.
Example Output:
/: 1.5 GiB (1610970624 bytes) trimmed
/home: 500 MiB (524288000 bytes) trimmed
/mnt/data: 2.5 GiB (2684354560 bytes) trimmed
Use Case 2: Trim unused blocks on a specified partition
sudo fstrim /
Motivation:
Sometimes there may be a need to trim unused blocks on only a specific partition instead of all mounted partitions. In this case, specifying the desired partition path as an argument with fstrim
allows us to trim only that selected partition.
Explanation:
By providing the partition path (e.g., /
) as an argument to fstrim
, we specify the exact partition on which we want to discard unused blocks. The command will identify and trim the unused blocks on the specified partition.
Example Output:
/: 1.5 GiB (1610970624 bytes) trimmed
Use Case 3: Display statistics after trimming
sudo fstrim --verbose /
Motivation:
Monitoring the effectiveness of the fstrim
command is important to understand how much space has been reclaimed. The --verbose
option provides detailed statistics about the trimming process, allowing us to track the amount of space freed.
Explanation:
The --verbose
option instructs fstrim
to display additional information about the trimming process. This includes the size of trimmed blocks in bytes and the total amount of space freed on the specified partition.
Example Output:
/: 1.5 GiB (1610970624 bytes) trimmed
In conclusion, the fstrim
command is a valuable tool for maintaining the performance and free space on flash memory devices. By utilizing the different use cases illustrated above, users can effectively discard unused blocks on both individual partitions and all supported mounted partitions, as well as monitor the results of the trimming process.