How to organize disk fragments in Linux

Source: Internet
Author: User

How to organize disk fragments in Linux

One myth is that Linux disks never need to be fragmented. In most cases, this is true, mostly because it uses excellent log systems (ext2, 3, 4, etc.) to process file systems. However, in some special cases, fragments are still generated. If it happens to you, the solution is simple.

 

What is disk fragmentation?

The file system updates files by block. If these blocks are not integrated but distributed in every corner of the disk, disk fragments are formed. This is true for FAT and FAT32 file systems. In NTFS, this situation is mitigated, but it does not happen in Linux (extX. The following are the reasons:

In file systems such as FAT and FAT32, files are immediately written to the disk. There is no space between files for growth or update:

NTFS reserves some space between files, so space increases. However, because the space between blocks is limited, fragments will also appear over time.

Linux's log-type file system uses a different solution. Different from files, each file is distributed across disks, leaving a large amount of space left between each file. This leaves a lot of space for file updates and growth, and fragments rarely occur.

In addition, when fragments occur, most Linux file systems attempt to reconnect files and blocks.

 

Disk sorting in Linux

Unless you use a small hard disk or insufficient space, Linux will rarely need disk sorting. Disk sorting may be required:

  • If you are editing large video files or RAW photos, but the disk space is limited
  • If you use an old-fashioned hardware such as an old laptop, your hard disk will be small.
  • If your disk is full (about 85%)
  • If your home directory contains many small partitions

The best solution is to buy a large hard drive. If it is not possible, disk fragment will be useful.

 

How to check fragments

fsckThe command will do this for you. In other words, if you can run it in LiveCD, it can be used for all unmounted partitions.

This is important: Running fsck in a mounted partition will seriously harm your data and disk.

You have been warned. Make a complete backup before you start.

Disclaimer: The authors and websites of this article will not be responsible for your files, Data, systems or other damages. You must take risks on your own. If you continue, you need to accept and understand this.

You should start to a live session (such as using an installed disk or a system rescue CD) and run it on the detached partition.fsck. To check whether there are any problems, run the following command with the root permission:

  1. fsck-fn [/path/to/your/partition]

You can run the following command to find the partition path

  1. sudofdisk-l

One of them runs in a mounted partition.fsck(Relatively) The security method is to use-nSwitch. This checks the read-only file system of the partition without writing anything. Of course, this does not guarantee high security. You should do so after creating a backup. In ext2, run

  1. sudofsck.ext2 -fn /path/to/your/partition

This results in a large amount of output. The cause of most errors is that the partition has been mounted. Finally, a shard-related information is provided.

If the fragmentation rate is greater than 20%, you should start to sort out your disk fragments.

 

How to organize fragments in Linux

What you need to do is to back up all your files and data to another hard disk (manually copy them), format the partitions, and then copy them back (do not use the backup software ). The log file system uses them as new files and places them neatly into the disk without generating fragments.

To back up your files, run

  1. cp-afv [/path/to/source/partition]/* [/path/to/destination/folder]

Remember the asterisk.

Note: copying large files or a large number of filesddMaybe it's the best. This is a very underlying operation that will replicate everything, including idle space and even the garbage left. This is not what we want, so it is best to usecp.

Now you only need to delete the source file.

  1. sudorm-rf [/path/to/source/partition]/*

Optional: You can use the following command to fill free space with zero fill. You can also use formatting to achieve this, but if you do not copy the entire partition but just copy a large file (which usually forms fragments), you should not use the formatting method.

  1. sudoddif=/dev/zero of=[/path/to/source/partition]/temp-zero.txt

Wait until it ends. You can usepvTo monitor the progress.

  1. sudoapt-get install pv
  2. sudopv-tpreb | of=[/path/to/source/partition]/temp-zero.txt

This is done. You only need to delete the temporary file for filling.

  1. sudorm[/path/to/source/partition]/temp-zero.txt

When you clear the free space (or skip this step ). Copy the file back and replace the firstcpCommand to flip:

  1. cp-afv [/path/to/original/destination/folder]/* [/path/to/original/source/partition]

 

Use e4defrag

If you want to installe2fsprogs,

  1. sudoapt-get install e2fsprogs

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

  1. sudo e4defrag /

The mounting is not guaranteed to succeed (You should not use your system when it runs), but it is much easier than copying all the files and then copying them back.

 

Summary

In linux, because of its log-type file system's effective data processing, there is little fragmentation. If you have fragments for any reason, the simple method is to re-allocate your disk, such as copying all files and copying them back, or usinge4defrag. However, it is important to ensure the security of your data. Therefore, before performing any operations that may affect all or most of your files, make sure that your files have been backed up to another safe place.

Via: https://www.maketecheasier.com/defragment-linux/

Author: Attila Orosz Translator: geekpi Proofreader: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

This article permanently updates the link address:

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.