How to defragment ext4 formats (file systems)

Moderator: Forum moderators

Post Reply
User avatar
bigpup
Moderator
Posts: 6993
Joined: Tue Jul 14, 2020 11:19 pm
Location: Earth, South Eastern U.S.
Has thanked: 911 times
Been thanked: 1528 times

How to defragment ext4 formats (file systems)

Post by bigpup »

Ext4 file systems are inherently designed to reduce fragmentation through advanced techniques such as extent-based allocation, which groups contiguous blocks of data together. Typically, ext4 file systems experience minimal fragmentation, making extensive defragmentation processes less critical.

There is a program for ext4 formats to defrag.
All Puppy Linux OS's should already have this program.

e4defrag

e4defrag performs online defragmentation, which means it defragments files while the system is running and the file system is mounted and in use. To be clear, we can safely defragment a file while another program is using it.

Use in a terminal.

Usage:
e4defrag [-v] file...| directory...| device...
e4defrag -c file...| directory...| device...

Target can be a regular file, a directory, or a device that is
mounted as ext4 file system. If target is a directory, e4defrag
reduces fragmentation of all files in it. If target is a device,
e4defrag gets the mount point of it and reduces fragmentation of
all files in this mount point.

It is a simple program to use, but does have a few options.

OPTIONS :

-c Get a current fragmentation count and an ideal
fragmentation count, and calculate fragmentation score
based on them. By seeing this score, we can determine
whether we should execute e4defrag to target. When used
with -v option, the current fragmentation count and the
ideal fragmentation count are printed for each file.

Also this option outputs the average data size in one
extent. If you see it, you'll find the file has ideal
extents or not. Note that the maximum extent size is
131072KB in ext4 file system (if block size is 4KB).

If this option is specified, target is never defragmented.

-v Print error messages and the fragmentation count before
and after defrag for each file.

The things you do not tell us, are usually the clue to fixing the problem.
When I was a kid, I wanted to be older.
This is not what I expected :o

User avatar
mikewalsh
Moderator
Posts: 6158
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: How to defragment ext4 formats (file systems)

Post by mikewalsh »

I always understood Ext file-systems rarely - if ever - required defragging (which bigpup's first quote confirms). On top of that, of course, more & more of us are moving to SSDs.......and with these, from what I understand, defragging is a total no-no anyway, because it rapidly accelerates cell wear if performed at all regularly.

Doesn't T.R.I.M and the in-built 'rubbish collection' for SSDs perform much the same function..?

Mike. ;)

vektor_alian
Posts: 54
Joined: Thu Apr 01, 2021 12:36 am
Been thanked: 13 times

Re: How to defragment ext4 formats (file systems)

Post by vektor_alian »

Hey everybody.

I like to try various distros by iso booting and had problems with fragmentation.
I have tried to make them contiguous and even tried --preallocate but no-go.
Probably should just wipe the test partition and start over.

Vektor

Post Reply

Return to “File Management”