Вы находитесь на странице: 1из 6

PHP 5/"My SQL" Program m in g for th e Absolute Begin n er - Free eBook

Download Now

LINUX COMMANDS

How to Repair and Defragment Linux System Partitions and Directories


BY MARIN TODOROV LAS T UPDATED: NOVEMBER 25, 2015

Download Your Free eBooks NOW - 10 Free Linux eBooks f or Administrators | 4 Free Shell Scripting eBooks

People who use Linux often think that it doesnt 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.

Defragment Linux System Partitions and Directories

The Linux ext2, ext3 and ext4 filesystems dont 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?


AdChoices

Linux Download
Linux Tutorial
Linux File System
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 its life. Instead, if
you are using SSD, you should use the TRIM function, which is not covered in this tutorial.
lets 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 its the safe way to go:

$ sudo umount <device file>

In my case I have /dev/sda1 mounted at /t mp:

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 unmount /dev/sda1

Now lets 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:

e2fsck Healthy Partition

Here is another example that shows errors on a system:

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
syst em errors left uncorrect ed. Depending on t he issue t hat has been found, different act ions might be
required.
If the issue appears on a partition that cannot be unmounted, you can use another tool called e4 defrag. It
comes pre-installed on many Linux distros, but if you dont have it on yours, you can install it with:

$ sudo apt-get install e2fsprogs


# yum install e2fsprogs
# dnf install e2fsprogs

[On Debian and Derivatives]


[On CentOS based systems]
[On Fedora 22+ versions]

How to Defragment Linux Partitions


Now its 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
Defragment at ion is an operation that you will rarely need to run in Linux. Its 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.

SHARE
318

219

LEAVE A

REPLY