Howto: Defrag your HDD

Share your own howto's etc. Not for support questions!
Post Reply
User avatar
Posts: 1192
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 4 times
Been thanked: 2 times

Howto: Defrag your HDD

#1 Post by Hallvor »

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/
Lenovo ThinkPad T440S, Intel Core i7-4600U CPU @ 2.10GHz, 8 GB RAM, 256 GB SSD, Debian Bullseye (KDE)
Lenovo ThinkPad X240, Intel Core i5-4300U CPU @ 2.90GHz, 8 GB RAM, 120 GB SSD, Debian Bullseye (KDE)

User avatar
Posts: 3064
Joined: 2013-03-12 07:18

Re: Howto: Defrag your HDD

#2 Post by debiman »

(couldn't resist)
(javascript required)

User avatar
Posts: 12408
Joined: 2009-10-06 05:53
Has thanked: 7 times
Been thanked: 5 times

Re: Howto: Defrag your HDD

#3 Post by stevepusser »

It could be worth noting that defragging a solid state drive serves no real purpose.
MX Linux packager and developer

Post Reply