Skip to content

Instantly share code, notes, and snippets.

@rmi1974
Last active February 26, 2026 08:06
Show Gist options
  • Select an option

  • Save rmi1974/108c58cdeaa4c1f8b29a2af68f6ca698 to your computer and use it in GitHub Desktop.

Select an option

Save rmi1974/108c58cdeaa4c1f8b29a2af68f6ca698 to your computer and use it in GitHub Desktop.
How to optimize / defrag ext4 filesystem #ext4 #commandlinefu

How to optimize / defrag ext4 filesystem

Make sure the target filesystem is sane

Open up the terminal and run:

sudo fsck.ext4 -y -f -v /dev/<disk/partition>

Optimize the directories

sudo fsck.ext4 -y -f -v -D /dev/<disk/partition>

Done. Note : Pass 3A: Optimizing directories.

It also (almost always) reports :

****** FILE SYSTEM WAS MODIFIED *****

Don't panic, as long the previous 'fsck' went off without a hitch, all should be golden. Run the 'fsck' (Without '-D' option) once more sometimes just to see how much it optimized.

Optimize files

Mount your filesystem now.

(Right-click on your disk on the desktop or wherever and 'mount')

After mounting:

sudo e4defrag -v /dev/<disk/partition>

Now, unmount again with :

sudo umount /dev/<disk/partition> && cd /

Finally to check fragmentation stats:

sudo fsck.ext4 -y -f -v /dev/<disk/partition>

Links

@rmi1974
Copy link
Author

rmi1974 commented Feb 26, 2026

I'm happy this gist was useful to you. 👍 This was knowledge gathering and sharing from before the age of AI. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment