Howto: Defrag your HDD

Share your own howto's etc. Not for support questions!

Howto: Defrag your HDD

Postby Hallvor » 2017-04-03 19:32

What is defragmentation?
In the maintenance of file systems, defragmentation is a process that reduces the amount of fragmentation. It does this by physically organizing the contents of the mass storage device used to store files into the smallest number of contiguous regions (fragments).

Does GNU/Linux need defragmentation?
Linux’s ext2, ext3, and ext4 file systems – ext4 being the file system used by Debian and most other current Linux distributions – allocates files in a more intelligent way than NTFS on Windows. Instead of placing multiple files near each other on the hard disk, Linux file systems scatter different files all over the disk, leaving a large amount of free space between them. When a file is edited and needs to grow, there’s usually plenty of free space for the file to grow into. If fragmentation does occur, the file system will attempt to move the files around to reduce fragmentation in normal use, without the need for a defragmentation utility.

Because of the way this approach works, you will start to see fragmentation if your file system fills up. If it’s 95% (or even 80%) full, you’ll start to see some fragmentation. However, the file system is designed to avoid fragmentation in normal use.

How can I see if my file system is fragmented?
As root, run the following command to check the root partition:

Code: Select all
# e4defrag -c /

I have already done a defrag, and obviously I don't have to do it again:

Code: Select all
hallvor@debian-hp:~$ su
root@debian-hp:/home/hallvor# e4defrag -c /
<Fragmented files>                             now/best       size/ext
1. /home/hallvor/.cache/chromium/Default/Cache/1513aefa62ebb471_0
                                                 2/1              4 KB
2. /home/hallvor/.cache/chromium/Default/Cache/7c4455bf7efed853_0
                                                 2/1              4 KB
3. /home/hallvor/.cache/chromium/Default/Cache/f7d205a293a15136_0
                                                 2/1              4 KB
4. /home/hallvor/.cache/chromium/Default/Cache/b6a709b230c92d44_0
                                                 2/1              4 KB
5. /home/hallvor/.cache/chromium/Default/Cache/3f0dbf6972000ce1_0
                                                 2/1              4 KB

 Total/best extents                             147279/145521
 Average size per extent                        1476 KB
 Fragmentation score                            0
 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
 This directory (/) does not need defragmentation.
root@debian-hp:/home/hallvor# exit

As you can see, the program will advise you to do a defrag once the fragmentation level gets too high.

If you want to defrag your HDD, just run the following command (as root), or choose a different location:
Code: Select all
# e4defrag /

Sources: ... agmenting/
HP Elitebook 2570p, Intel Core i5 3320-M CPU @ 2.60 GHz, 6 GB RAM, Intel HD 4000 graphics, 240 GB SSD, Debian Stretch (KDE)
User avatar
Posts: 853
Joined: 2009-04-16 18:35
Location: Norway

Re: Howto: Defrag your HDD

Postby debiman » 2017-04-04 18:06

(couldn't resist)
(javascript required)
User avatar
Posts: 3064
Joined: 2013-03-12 07:18

Re: Howto: Defrag your HDD

Postby stevepusser » 2017-04-04 18:26

It could be worth noting that defragging a solid state drive serves no real purpose.
The MX Linux repositories: Backports galore! If we don't have something, just ask and we'll try--we like challenges. New packages: Flightgear 2018.2.2, 4.19.13 kernel, wine-staging 4.0~rc4, Stellarium 0.18.3, VLC 3.0.5, Midori 7.0
User avatar
Posts: 10437
Joined: 2009-10-06 05:53

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 7 guests