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.
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
fsck CommandThis will be done for you. In other words, if you can run it in LiveCD, it can be usedAll 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 the installation disk, system rescue CD, etc.) andUninstallRun on the partitionfsck. To check whether there are any problems, run the following command with the root permission:
fsck -fn [/path/to/your/partition]
You can run the following command to find the partition path
sudo fdisk -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
sudo fsck.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 back up youAllTo another hard disk (manuallyCopyThey), format the partition, and copy it back again (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
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.
sudo rm -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.
sudo dd if=/dev/zero of=[/path/to/source/partition]/temp-zero.txt
Wait until it ends. You can usepvTo monitor the progress.
sudo apt-get install pvsudo pv -tpreb | of=[/path/to/source/partition]/temp-zero.txt
This is done. You only need to delete the temporary file for filling.
sudo rm [/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:
cp -afv [/path/to/original/destination/folder]/* [/path/to/original/source/partition]
Use e4defrag
If you want to installe2fsprogs,
sudo apt-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:
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.