How to defragment a disk in Linux

Source: Internet
Author: User

There is a myth that Linux disks never need to be defragmented. In most cases this is true, mostly because of the use of excellent log file systems (EXT3, 4, and so on) to handle file systems. However, in some special cases, the fragments will still be produced. If it happens to you, the solution is simple.

What is disk fragmentation

The file system updates the file by block, and if the blocks are not connected to a whole block but are distributed across the disk, disk fragmentation is formed. This is true for the FAT and FAT32 file systems. This is somewhat mitigated in NTFS, but it is unlikely to happen in Linux (EXTX). Here's why:

In file systems such as FAT and FAT32, files are written to disk next to each other. There is no space between files for growth or for updates:

NTFS Space is reserved between files, so there is room for growth. However, because space between blocks is limited, fragments can also occur over time.

Linux A different scenario is used for the journaled file system. Unlike files, each file is distributed across the disk, leaving a significant amount of space left between each file. This leaves a lot of room for file updates and growth, and fragmentation rarely happens.

In addition, once a fragment is present, most Linux file systems attempt to re-sequence the files and blocks.

Linux disk Grooming in

Linux rarely requires disk grooming unless you're using a small hard drive or not enough space. Some scenarios that may require disk grooming include:

If you're editing a large video file or RAW photo, but disk space is limited

If you use an old-fashioned hardware, older notebooks, your hard drive will be very small

If your disk starts to fill up (approximately 85% used)

If you have many small partitions in your home directory

The best solution is to buy a large hard drive. If this is not possible, disk defragmentation can be useful.

How to check for fragmentation

fsck The command will do this for you, in other words, if you can run it in LiveCD, then you can use it for all the unloaded partitions.

This is important: running fsck in a mounted partition can seriously compromise your data and disk.

You've been warned. Before you begin, make a full backup.

Disclaimer: The author and site of this article will not be responsible for your documents, data, system or other damages. You need to take risks yourself. If you continue, you need to accept and understand this.

You should boot to a live session (such as using an installation disk, a system rescue CD, etc.) and run fsck on the partition you uninstalled. To check for any problems, run the following command with root privileges:

FSCK-FN [/path/to/your/partition]

You can run the following command to find the path to the partition

sudo fdisk-l

One way to run fsck (relative) security in a mounted partition is to use the-n switch. This will make a read-only file system check of the partition without writing anything. Of course, this is not guaranteed to be very secure, you should do it after creating a backup. In Ext3, run

sudo fsck.ext3-fn/path/to/your/partition

This produces a lot of output, and most of the error information is because the partition is already mounted. Finally, a fragment-related information is given.

If the fragmentation rate is greater than 20%, then you should start defragmenting your disk.

How to easily defragment a piece of Linux

All you have to do is back up all your files and data to another hard drive (copy them manually), format the partition, and copy it back (don't use backup software). The journaled file system takes them as new files and neatly places them on disk without fragmentation.

To back up your files, run

CP-AFV [/path/to/source/partition]/* [/path/to/destination/folder]

It is important to remember the asterisk (*).

Note: It is generally considered best to copy large files or a large number of files, using DD. This is a very low-level operation, it will copy everything, including free space or even the rubbish left behind. This is not what we want, so it's best to use CP here.

Now you just need to delete the source file.

sudo rm-rf [/path/to/source/partition]/*

options available : You can use the following command to populate the free space with 0. You can also use formatting to achieve this, but you should not use a formatted method if you do not copy the entire partition and simply copy large files (which usually form fragments).

sudo dd If=/dev/zero of=[/path/to/source/partition]/temp-zero.txt

Wait for it to end. You can use PV to monitor progress.

sudo apt-get install PV

sudo Pv-tpreb | Of=[/path/to/source/partition]/temp-zero.txt

This is done, just delete this temporary file for padding.

sudo rm [/path/to/source/partition]/temp-zero.txt

Until you clear the free space (or skip this step). Re-copy back to the file and flip the first CP command:

CP-AFV [/path/to/original/destination/folder]/* [/path/to/original/source/partition]

Using E4defrag

If you want an easy way to install E2fsprogs,

sudo apt-get install E2fsprogs

Run E4defrag in the partition with root privileges. If you do not want to or cannot unload the partition, you can use its mount point instead of the path. To defragment the entire system, run:

sudo e4defrag/

There is no guarantee of success in the case of mounting (you should also not use your system while it is running), but it is much simpler than copying all the files and re-copying them back.

Summarize

Linux The system is rarely fragmented due to its efficient data processing in the journaled file system. If you have fragmented for any reason, the easy way is to reassign your disk, such as copying all the files and copying them back, or using E4defrag. It's important to keep your data safe, so make sure your files are backed up to another safe place before you do anything that could affect all or most of your files.

free pick up brother Lian it education original Linux OPS engineer video / detail linux tutorial, For more information, please contact: http://www.lampbrother.net/linux /

or hooking up with Q2430675018.

Welcome to the Linux Communication Group 478068715


How to defragment a disk in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.