Page 1 of 1

Defragmenting on Linux

Posted: 30 Nov 2015, 18:53
by viking60
Yes I know: You don't have to defrag Linux. Mostly mechanisms are built in to take care of it so it will never be absolutely necessary. That does not mean that Linux does not get slightly fragmented though.
It is possible to defrag Linux with Exr2 Ext3 and Ext4 systems though.
To check your filesystems you can do a:

Code: Select all

df -T


Just boot from the Viking Rescue system and start Systemrescuecd.
Check your Partitions:

Code: Select all

fdisk -l

And make a mount-point

Code: Select all

mkdir /mnt/sda1

(My Linux partition was /dev/sda1)
Mount it:

Code: Select all

mount /dev/sda1 /mnt/sda1

Start this command to defrag all your directories:

Code: Select all

e4defrag /mnt/sda1 /


Don't use this on SSD only on HDD.

You may not have to unmount the partition for this so you could try it directly in your terminal.

Code: Select all

e4defrag /home/viking


To repair filesystems you need to unmount the partition so our Rescue System comes in handy.
Fire up Systemrescuecd and do a

Code: Select all

fdisk -l
If the answer is /dev/sda1 then you can check your fragmentation with:

Code: Select all

e2fsck -fn /dev/sda1

It will probably be clean and come up with no fragmentation.

But if this reports fragmentation you can repair it with:

Code: Select all

e2fsck -p /dev/sda1

After that your read write process might be faster.

Again: this works for Ext2 3 and 4 and should not be used on anything other than traditional HDD's - never on SSD.

This is for fine tuning pros - you never actually need to do this I was just bored: so I though I'd write down a tip :-D

Re: Defragmenting on Linux

Posted: 30 Nov 2015, 18:55
by viking60
Btrfs distros like Opensuse do have built in defrag options too:
:A
viewtopic.php?f=3&t=3619&p=19931

Re: Defragmenting on Linux

Posted: 01 Dec 2015, 01:22
by dedanna1029
Good job.

I will have to find an old link that I had at one time a few years back on defragmenting a reiserfs system (of which I'm still on, it's my "trusty"). I had thought about trying it out so many times then never did lol. It may be next on the agenda, well, after a few other things. :lol:

Re: Defragmenting on Linux

Posted: 01 Dec 2015, 13:35
by viking60
This can be handy when your partition is getting full - like in my case - because there is less space to move things around.

Re: Defragmenting on Linux

Posted: 02 Dec 2015, 16:29
by dedanna1029
Indeed. I'm also interested in finding out about doing a defrag on Windows from Linux. You know Linux would do it better. ;)