Close Menu
    X (Twitter)
    Hectic GeekHectic Geek
    • Apps
      • Apps for Mac
      • Apps for PCs
      • Best Apps
    • Concepts
      • Comparison
      • For Geeks
      • IT Careers
    • Gadgets
      • Accessories
      • Best Work Laptops
      • Budget Laptops
    • GNU/Linux
      • Ubuntu
    • How To
      • Virtualization
      • Windows
      • Windows How-To
    Hectic GeekHectic Geek
    Home » Defragment ‘Ext4’ File systems Using ‘e4defrag’ (Ubuntu)
    Ubuntu

    Defragment ‘Ext4’ File systems Using ‘e4defrag’ (Ubuntu)

    GayanBy GayanOctober 24, 2012Updated:April 30, 201522 Comments6 Mins Read
    Twitter Reddit Telegram WhatsApp Email
    Share
    Twitter Reddit Telegram WhatsApp Email

    Different operating systems use different file systems. The GNU/Linux for instance, has a lot of file systems, but primarily relies on the ‘Ext4’ (which will eventually be replaced by ‘Btrfs’). And when talking about file systems, another thing that comes into mind, as it has a direct effect on the performance of the file system, is the term, ‘fragmentation’.

    Because, no matter what file system that your operating system uses, it is inevitable that it too will be fragmented, as time goes on. And the main reason for file system fragmentation is the complications that occur while trying to find ‘nearby’ or ‘continuous’ free space areas (‘blocks’).

    This is because, if it was a program, and the OS was able to store its data in nearby blocks, then while opening it later, finding those blocks that hold the program on the HDD is easy, as they are, nearby (‘continues blocks’).

    But, while trying to store the program, if the OS was unable to find nearby blocks and its data got scattered all over the HDD (which is a common scenario as you add/remove data, unlike in a newly formatted partition, it destroys the continuum of the free space), then it will take more time to load as the HDD’s ‘head’ (the mechanical tool that read/write data to sectors) has to look all over the place for finding (reading) them.

    Some say the GNU/Linux file systems, such as ‘Ext4’, does not need any fragmentation (unlike with Windows file systems), as it is designed in such a way, it minimizes the fragmentation.

    ‘e4defrag’ in action …

    This is, to some extend, is true. But, there are experts like ‘Takashi Sato’ who has done many experiments using few major GNU/Linux file systems and their file fragmentation’s effect on the performance.

    He has written an excellent PDF called ‘ext4 online defragmentation’ (a more up-to-date one can be found here) and makes the following conclusions at the end:

    Fifty fragmented 1GB files were created (Edit: using ‘ext4’). Read performance was measured before and after defragmentation …

    … Performance measurement has shown that defragmentation for a single file can improve read performance by 25% on a fragmented 1GB file. For relevant file fragmentation, defragmentation resulted in 29% performance improvement for accessing all file in the Linux source tree.

    Online defragmentation is a promising feature to improve performance on a large filesystems such as ext4 …

    He and ‘Akira Fujita’ has created a simple and easy to use command-line based tool that lets us defragment any ‘ext4’, for improving the performance. It is called ‘e4defrag’ and if you are using Ubuntu 12.04 LTS, 12.10 Quental Quetzal or newer, then it is already installed in the OS.

    But remember, this only works in the ‘Ext4’ file system and not in any other.

    How to use it?

    Very simple. Open your Terminal and enter the below command.

    e4defrag /location

    The ‘location’ can be anything from a mounted ‘ext4’ device (including the root file system, ‘/’) to any individual file or a folder inside an ‘ext4’ file system.

    Getting a ‘report’ …

    Another cool feature of ‘e4defrag’ is that, it also has the ability to scan a mounted ‘ext4’ file system (or any location inside it) and give you a ‘report’ that includes a value called ‘fragmentation score’.

    And based on this ‘fragmentation score’, ‘e4defrag’ tells you whether you should defrag that location or not!.

    Note: If the score in the output is within 0-30 then you do not need to defrag. If it says 31-55, then it is a bit fragmented and if it is 56, then you should fragment the location.

    This is a newly installed Ubuntu 12.10 and I ran ‘e4defrag’ before creating this summary, which is why the score is ‘0’, if I’m not mistaken …

    For that you can use it in the below format (make sure to replace ‘/location’ with your file path).

    e4defrag -c /location

    Here are few examples …

    Using it on the ‘Home’ folder …

    If you wanted to get a ‘fragmentation score’ for the ‘Home’ folder first and then defragment it later, then use the below two commands.

    sudo e4defrag -c ~/

    sudo e4defrag ~/

    (Here, there’s no need to change anything, the ‘~/’ will automatically add your ‘Home’ folder path).

    Using it on the ‘root’ file system …

    If you wanted to get a ‘fragmentation score’ for the root file system (including the OS), and defrag it later, then use the below two commands (if you have a separate ‘/boot’ partition, never use it on it!).

    sudo e4defrag -c /

    sudo e4defrag /

    Note: While the defragmentation is done, it will give you a basic output. And in this output, it is normal to have values under ‘Failure’, as some of the core system files are unmovable.

    Please remember that, in GNU/Linux, all of your files are mounted on the ‘root filesystem’ (‘/’). So if you had more than one ‘ext4’ file systems mounted, then they too will be defragmented, thus, depending on their files and their sizes etc, it might take a long time.

    Deframenting mounted ‘ext4’ partitions manually?

    If you have more than one ‘ext4’ partition and wanted to defrag them individually, but don’t know their ‘device name’ (/dev/sda …), then you can use the below steps for that.

    1. Enter the below command in your Terminal to get a list of current partitions and their file systems.

    sudo fdisk -l /dev/sda

    2. Then in the output, under the ‘Id’ field, if for a partition it says ’83’, then that is an ‘ext4’ file system. Then for that line, look under the ‘Device’ and whatever lies there, is the device location of that ‘ext4’ partition (in this example, I have two, ‘sda5’ and ‘sda6’).

    3. Then simply copy that name under ‘Device’, and replace it with the ‘sda’ part in the below command.

    sudo e4defrag /dev/sda

    Depending on the partition (whether its where the OS is installed etc), you might or might not need to use ‘sudo’.

    If you want to know more, then you can read the short manual of ‘e4defrag’, by using the below command.

    man e4defrag

    That’s it. Good luck.

    Share. Twitter Reddit Telegram WhatsApp Email
    Gayan

    An RHCE, 'Linux' user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.

    Related Posts

    How to Check Linux CPU Usage?

    February 5, 2024

    Linux export Command: A Comprehensive Tutorial

    February 3, 2024

    Debian vs Ubuntu Server: A Brief Comparison for Beginners

    July 2, 2023

    CPU Based Simple System Stability and Benchmark Tester for Ubuntu Linux – systester

    June 16, 2023

    Typespeed: Typing Speed Testing Game for Ubuntu Linux

    March 12, 2023

    Debian vs Ubuntu in 2023- Which Linux OS is Right for You?

    January 6, 2023

    22 Comments

    1. jeremia777 on October 30, 2012 9:37 pm

      There is GUI for e4defrag (i think):
      https://apps.ubuntu.com/cat/applications/hdd-ranger/

      Reply
      • Gayan on November 1, 2012 8:16 am

        Thanks ‘jeramia777’. I actually saw that too, but because it isn’t free, I couldn’t review it :).

        Reply
        • Marcello on June 19, 2020 11:10 pm

          excellently written – what a joy reading this nice, informative, relatively long/short article ; no need for a GUI in my eyes *** thank you so much and love & peace

          Reply
      • Aprox on January 15, 2013 5:30 pm

        I just buy it for my family Ubuntu laptop. It is working well, thanks for that link buddy!

        Reply
    2. Rafi B. on November 13, 2012 10:29 pm

      Hi Gayan, great article!
      A little typo: “and if you are using […] or never” — did you mean newer? 🙂

      Reply
      • Gayan on November 14, 2012 8:05 am

        Thanks ‘Rafi’, I corrected it. I don’t know why, but it happens every now and then ;-).

        Reply
    3. Jan on March 4, 2013 9:32 pm

      Thanks for the howto.
      Just a remark: running ‘e4defrag -c’ on the home folder gives no output (except for ‘Done.’), normal users are not authorized to see any. You have to use sudo even on your home folder to see the fragmentation count.

      Reply
      • Gayan on March 5, 2013 3:27 pm

        Hi Jan,

        Thank you very much mate!. I updated the post :).

        Reply
    4. Anthony Venable on April 27, 2013 2:04 am

      Thanks so much for this!!! I had a conversation with one of my brothers and I felt as thought it was unnecessary but now I get that it is, although less likely to be an issue than with Windows apparently.

      Reply
      • Gayan on April 27, 2013 6:30 am

        You are welcome ‘Anthony’, & thank you for the link also :).

        Reply
    5. BT on December 28, 2013 12:14 am

      I stumbled upon this post while looking for info about e4defrag in DuckDuckGo> : it turns out it is not only the most up-to-date result (apart the Ubuntu man page>), but it is also an extremely easy to read and easy to understand guide for this little great program. Many thanks for that to the author ! 🙂

      PS : sorry for the double post, I messed up with the HTML tags in the first one. Could you delete my previous comment and the notice in this one when it’s done? Thanks!

      Reply
      • Gayan on December 28, 2013 7:49 am

        You’re welcome BT :).

        Reply
    6. Knut on January 28, 2015 1:36 am

      It seems like the author of the article and the software does not understand how the “ext4” file system works. It is not inevitable that a file system has to be come cluttered and fragmented. It is fully possible to make a file system that does not clutter like NTFS. Now the Linux file systems will avoid fragmenting and cluttering, and will work in the background to clean up any mess, It is first when there are few spare blocks available that the file system will become cluttered – typically when you have used about 85% of the space on the disk. This is not Windows, and a lot of effort has been done to make the file system blistering fast. On top of that, the file system is journaled, so should the app crash or power plug pulled, the file system will always be consistent and come up consistent.. And “defragmentation” will not happen unless the device is more than 85% full.

      Reply
    7. Torin Doyle on April 26, 2015 10:11 pm

      For this part of the article:

      sudo e4defrag -c ~/
      sudo e4defrag ~/

      ..should the following be used instead? Since it is defragging the home dir and not the root dir.

      e4defrag -c ~/
      e4defrag ~/

      Reply
      • Gayan on April 27, 2015 12:37 pm

        Hi Torin,

        If you want to defrag the root file system, then you should use it as below:

        e4defrag /

        If you want to get a more richer output (before and the after ‘fragment scores’ etc) then use it with the ‘-v’ argument:

        e4defrag -v /

        Please remember that, be very careful while using this utility with administrative (root) privileges, it can cause you troubles!. For instance, if you have a separate ‘boot’ partition, and if you degrag that (by specifically pointing to it: ‘e4defrag /boot’), then your system might fail to boot!. but I’ve used it to defrag the root file system and it haven’t given me any issues, and it simply ignores the files (say the ones that are associated with system boot) that shouldn’t be moved.

        P.S: I put in the article not the ‘-‘ sign, but ‘~’ which refers to the currently logged in user’s home directory.

        Reply
        • Torin Doyle on April 28, 2015 1:37 am

          Hi Gayan.

          But if I only want to defrag my home directory — must I still use sudo?

          Reply
          • Gayan on April 28, 2015 7:05 am

            No, you can (and should) use it without ‘sudo’ :).

            Reply
    8. Torin Doyle on April 29, 2015 10:42 pm

      Thank you Gayan for clarifying that.

      Reply
      • Gayan on April 30, 2015 7:58 am

        You’re welcome Torin.

        Reply
    9. GreenLeaf on June 30, 2015 10:40 am

      Do I do something wrong or do I have to very… VERY patient?? I entered the command “sudo e4defrag -c /” and nothing happens except for a blinking terminal cursor. I am using Linux Mint 17.1. Any advice what I should do?

      Reply
      • Gayan on June 30, 2015 5:05 pm

        Hi,

        When you use the ‘-c’ argument, ‘e4defrag’ simply scans the system and gives you a fragmented count, and that’s all it does. To defrag, use ‘e4defrag’ without it (‘-c’). If you prefer a more rich output, then use the ‘-v’ argument. For instance:

        sudo e4defrag -v /

        Reply
    10. Programowanie on November 11, 2020 6:37 pm

      It is posible to use this command on chromebook? on my chromebook this command don’t work(this filesystem is non ext4). In ubuntu work corectly!

      Reply
    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Latest Posts

    Transforming Geek Culture with Advanced Software Development

    April 8, 2024

    The Role of Technology in Enhancing Digital Signage Solutions

    February 21, 2024

    How to Check Linux CPU Usage?

    February 5, 2024

    Linux export Command: A Comprehensive Tutorial

    February 3, 2024

    [Fixed] HDMI No Signal to TV or Monitor on Windows 10/11

    November 7, 2023

    AlmaLinux vs Ubuntu – How to Select the Best One?

    October 22, 2023

    Subscribe to Updates

    Get the latest news and updates from Hectic Geek. We won't overload you or sell your data!

    Contact
    • About Us
    • Contact Us
    • Privacy Policy
    • Write for Us
    X (Twitter)
    © 2026 Hectic Geek.

    Type above and press Enter to search. Press Esc to cancel.