Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to defragment a disk in Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to defragment disks in Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to defragment disks in Linux.

What is disk fragmentation?

The file system updates files by block, and if these blocks are not joined together but are distributed in all corners of the disk, they will form disk fragments. This is true for FAT and FAT32 file systems. This situation alleviates somewhat in NTFS, but rarely happens in Linux (extX). Here are the reasons:

In file systems like FAT and FAT32, files are written to disk next to each other. There is no space between files to grow or update:

NTFS leaves some space between files, so there is room to grow. However, because the space between blocks is limited, fragments will appear over time.

Linux's journaling file system adopts a different scheme. Unlike files next to each other, each file is distributed all over the disk, leaving a lot of remaining space between each file. This leaves a lot of room for file updates and growth, and fragments rarely occur.

In addition, once fragmentation occurs, most Linux file systems try to concatenate files and blocks again.

Disk demarcation in Linux

Unless you are using a small hard drive or there is not enough space, Linux will rarely need to clean up the disk. Some situations that may require disk defragmentation include:

◆ if you are editing a large video file or RAW photo, but the disk space is limited

◆ if you use an old hardware, such as an old laptop, your hard drive will be very small

◆, if your disk starts to fill up (about 85% used)

◆ if there are many small partitions in your home directory

The solution is to buy a large hard drive. If that's not possible, disk defragmentation is useful.

How to check for fragments

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

This is important: running fsck on a mounted partition will seriously harm your data and disks.

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

Disclaimer: the author of this article and this site will not be responsible for your files, data, system or other damage. You need to take your own risks. If you continue, you need to accept and understand this.

You should start into a live session (such as using the installation disk, system rescue CD, etc.) and run fsck on the partition you uninstalled. To check if there are 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 (relatively) safe way to run fsck in a mounted partition is to use the-n switch. This does a read-only file system check on the partition without writing anything. Of course, this is not guaranteed to be completely safe, and you should do it after you create a backup. In ext2, run:

Sudo fsck.ext2-fn / path/to/your/partition

This produces a large amount of output, and most of the error messages are due to the fact that the partition is already mounted. * * will give a fragment-related information.

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

How to simply defragment in 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 then copy it back (do not use backup software). The journal file system treats them as new files and places them neatly 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 believed that it is possible to use dd to copy large files or a large number of files. This is a very low-level operation that replicates everything, including free space and even garbage left behind. This is not what we want, so cp is used here.

Now you just need to delete the source file.

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

Optional: you can fill the free space with zeros using the following command. You can also do this with formatting, but you should not use formatting if you do not copy the entire partition but only large files (which are usually fragmented).

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

Waiting for it to end. You can use pv to monitor progress.

Sudo apt-get install pvsudo pv-tpreb | of= [/ path/to/source/partition] / temp-zero.txt

This is done, just delete the temporary file for filling.

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

After you clear the free space (or skip this step). Copy back to the file and flip * cp commands:

Cp-afv [/ path/to/original/destination/folder] / * [/ path/to/original/source/partition]

Use e4defrag

If you want an easy way, install e2fsprogs.

Sudo apt-get install e2fsprogs

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

Sudo e4defrag /

Success is not guaranteed in the case of mount (you should also not use your system while it is running), but it is much easier than copying all the files and copying them back.

Summary

There are few fragments in the linux system because of the efficient data processing of its journal file system. If you have fragmentation for any reason, the simple way is to redistribute your disk, such as copying all the files out and copying them back, or using e4defrag. However, it is important to keep your data secure, so before doing anything that may affect all or most of your files, make sure your files have been backed up to another safe place.

At this point, I believe you have a deeper understanding of "how to defragment disks in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report