• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How to Repair and Defragment Linux System Partitions and Directories

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
61
VIEWS
Share on FacebookShare on Twitter

Contents

  1. How to Check a Filesystem Requires Defragmentation?
  2. How to Repair Linux Filesystem Using e2fsck
  3. How to Defragment Linux Partitions
  4. How to Defragment Linux Directory
  5. How to Defragment All Linux Partitions
  6. Conclusion

People who use Linux often think that it doesn’t require defragmentation. This is a common misunderstanding across Linux users. Actually, the Linux operating system does support defragmentation. The point of the defragmentation is to improve I/O operations like allowing local videos to load faster or extracting archives significantly faster.

How to Repair and Defragment Linux System Partitions and Directories

Defragment Linux System Partitions and Directories

The Linux ext2, ext3 and ext4 filesystems don’t need that much attention, but with time, after executing many many many read/writes the filesystem may require optimization. Otherwise the hard disk might become slower and may affect the entire system.

In this tutorial I am going to show you few different techniques to perform defragmentation on files. Before we start, we should mention what the common filesystems like ext2,3,4 do to prevent fragmentation. These filesystems include technique to prevent the effect. For example filesystems reserve free block groups on the hard disk to store growing files completely.

Unfortunately the problem is not always solved with such mechanism. While other operating systems may require expensive additional software to resolve such issues, Linux has some easy to install tools that can help you resolve such problems.

How to Check a Filesystem Requires Defragmentation?

Before we start I would like to point that the operations below should only be ran on HDDs and not on SSD. Defragging your SSD drive will only increase its read/write count and therefore shorten it’s life. Instead, if you are using SSD, you should use the TRIM function, which is not covered in this tutorial.

let’s test if the system actually requires defragmentation. We can easily check this with tool such as e2fsck. Before you use this tool on a partition on your system, it is recommended to unmount that partition with. This is not completely necessary, but it’s the safe way to go:

$ sudo umount <device file>

In my case I have /dev/sda1 mounted at /tmp:

How to Repair and Defragment Linux System Partitions and Directories

Disk Partition Table Before

Keep in mind that in your case the partition table might be different so make sure to unmount the right partition. To unmount that partition you can use:

$ sudo umount /dev/sda1

Now let’s check if this partition requires defragmentation, with e2fsck. You will need to run the following command:

$ sudo e2fsck -fn /dev/sda1

The above command will perform a file system check. The -f option forces the check, even if the system seems clean. The -n option is used to open the filesystem in read-only and assume answer of "no" to all questions that may appear.

This options basically allows to use e2fsck non-interactively. If everything is Okay, you should see result similar to the one shown on the screenshot below:

How to Repair and Defragment Linux System Partitions and Directories

e2fsck Healthy Partition

Here is another example that shows errors on a system:

How to Repair and Defragment Linux System Partitions and Directories

e2fsck With Errors

How to Repair Linux Filesystem Using e2fsck

If errors appear, you can attempt a repair of the filesystem with e2fsck with the “-p” option. Note that in order to run the command below, the partition will need to be unmounted:

$ sudo e2fsck -p <device file>

The “-p” options attempts automatic repair on the file system for problems that can be safely fixed without human intervention. If a problem is discovered that may require the system administrator to take additional corrective action, e2fsck will print a description of the problem and will exit with code 4, which means “File system errors left uncorrected”. Depending on the issue that has been found, different actions might be required.

If the issue appears on a partition that cannot be unmounted, you can use another tool called e4defrag. It comes pre-installed on many Linux distros, but if you don’t have it on yours, you can install it with:

$ sudo apt-get install e2fsprogs         [On Debian and Derivatives]
# yum install e2fsprogs                  [On CentOS based systems]
# dnf install e2fsprogs                  [On Fedora 22+ versions] 

How to Defragment Linux Partitions

Now it’s time to defragment Linux partitions using following command.

$ sudo e4defrag <location>
or
$ sudo e4defrag <device>

How to Defragment Linux Directory

For example, if you wish to defragment a single directory or device, you can use:

$ sudo e4defrag /home/user/directory/
# sudo e4defrag /dev/sda5

How to Defragment All Linux Partitions

If you prefer to defragment your entire system, the safe way of doing this is:

$ sudo e4defrag /

Keep in mind that this process may take some time to be completed.

Conclusion

Defragmentation is an operation that you will rarely need to run in Linux. It’s meant for power users who know what exactly they are doing and is not recommended for Linux newbies. The point of the whole action is to have your filesystem optimized so that new read/write operations are performed more efficiently.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

DEBUGFS Command – Show File Creation Times in Linux

Next Post

6 Useful X-based (Gui Based) Linux Commands – Part II

Next Post

6 Useful X-based (Gui Based) Linux Commands – Part II

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet