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 disk in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to defragment the disk in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In fact, disk fragments should be called file fragments because files are saved to different locations on the entire disk, rather than in a continuous cluster of disks.

When the physical memory required by the application is insufficient, the operating system usually generates temporary swap files on the hard disk. The disk space occupied by this temporary swap file is used to form virtual memory. The manager of virtual memory will often read and write on the hard disk and produce a lot of fragmentation. This is the main reason for the fragmentation of hard drives. In addition, temporary files in the system can cause a lot of fragmentation. Temporary files are generated when IE browsers browse information or when users set them directly. Frequent disk defragmentation can improve the service efficiency of computer hard disk.

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 disk will be very small if your disk starts to be full (about 85%) if you have many small partitions in your home directory

The best 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 ext3, run

Sudo fsck.ext3-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. Finally, a fragment-related information will be given.

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 best 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 it is best to use cp 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 the first cp command:

Cp-afv [/ path/to/original/destination/folder] / * [/ path/to/original/source/partition] uses 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.

The above is all the contents of the article "how to defragment disks in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report