To defragment mechanical hard disk

Message
Author
Domenico
Posts: 10
Joined: Tue Feb 27, 2024 5:13 am

To defragment mechanical hard disk

#1 Post by Domenico »

Good morning,
on my PC with MX Linux I have a mechanical hard disk. What should I do to defragment it?
Thanks.

User avatar
Eadwine Rose
Administrator
Posts: 14424
Joined: Wed Jul 12, 2006 2:10 am

Re: To defragment mechanical hard disk

#2 Post by Eadwine Rose »

Nothing. It is not windows, defragmenting is not needed.

"File systems used by Linux don't generally need defragging because they're better designed than NTFS. They don't 'defrag in the background', they just do a better job of allocating files into available space.

You can defrag ext4 with the e4defrag command. Use -c to check for fragmentation. You'll generally be wasting your time." (found on Reddit)
MX-23.6_x64 July 31 2023 * 6.1.0-34amd64 ext4 Xfce 4.20.0 * 8-core AMD Ryzen 7 2700
Asus TUF B450-Plus Gaming UEFI * Asus GTX 1050 Ti Nvidia 535.216.01 * 2x16Gb DDR4 2666 Kingston HyperX Predator
Samsung 870EVO * Samsung S24D330 & P2250 * HP Envy 5030

User avatar
chrispop99
Global Moderator
Posts: 3323
Joined: Tue Jan 27, 2009 2:07 pm

Re: To defragment mechanical hard disk

#3 Post by chrispop99 »

Just to add to the advice from @Eadwine Rose, Windows doesn't need manual defragmentation these days; it does it automatically.

Chris
MX Facebook Group Administrator.
Home-built desktop - Core i5 9400, 970 EVO Plus, 8GB
DELL XPS 15
Lots of test machines

Stuart_M
Posts: 690
Joined: Wed Aug 07, 2019 7:10 pm

Re: To defragment mechanical hard disk

#4 Post by Stuart_M »

FAT and FAT32 write files right next to each other so there is no room left for file growth. NTFS leaves a little more room between files and that allows room for growth so, because space is limited, fragmentation will occur over time.

Defragmentation in Linux should not be a problem under normal conditions. Problems can occur when the disk/partition is almost full and there will be a contiguous area large enough to allocate a file. Linux journaling filesystems place each file scattered all over the disk leaving a generous amount of space between files. This leaves enough room for file updates which is why fragmentation rarely occurs with ext4. If fragmentation does happen, most Linux filesystems will attempt to shuffle files and chunks around to make them contiguous again.

To see if fragmentation is a problem in Linux using ext4, run the command in the below code window. Ensure "sudo" is used to see the "score" information. If the score is 0-30 then you do not need to defrag. 31-55 means that it is a bit fragmented, and if >=56 then you should defragment the location. See the man page for e4defrag for more information.

Remove the -c option if you want to defragment the location, which is given as root (/) in the below command:

Code: Select all

sudo e4defrag -c /

Domenico
Posts: 10
Joined: Tue Feb 27, 2024 5:13 am

Re: To defragment mechanical hard disk

#5 Post by Domenico »

Thank you all

User avatar
Kermit the Frog
Posts: 626
Joined: Mon Jul 08, 2024 8:52 am

Re: To defragment mechanical hard disk

#6 Post by Kermit the Frog »

One more thing: If it's ntfs you can even use an ext4 partition to defrag it, rather than any software in Windows etc. Just copy or move data to ext4, then copy or move back to ntfs :)

Post Reply

Return to “Hardware /Configuration”