The myth that Linux systems never need to defragment the disk is believed to have been heard by many people. Because Linux uses a good log file system (ext2, Ext3, Ext4, Btrfs, etc.), in most cases it does not need to defragment the disk.
What is a disk fragmentation file?
When a file system reads and writes files on a disk's sector, it forms a discontinuous whole, resulting in a disk fragmentation file. This is especially true for fat and fat 32 file systems, which are mitigated by the NTFS file system. So why does this rarely happen in Linux operating systems (EXTX)?
In the FAT and FAT32 file systems, files are written on the right side of each other's disks, and there is no room for file growth or updates.
NTFS leaves a partial interval for adjacent files, allowing the file to grow and update space. However, this interval is limited, and the time-to-use growth will result in disk fragmentation.
Linux's log file system uses a completely different storage method, directly storing each file on disk, leaving a free space between each file, and therefore rarely producing disk fragmentation files.
In addition, how disk fragmentation begins to occur, most Linux file systems will automatically attempt to reorganize files and blocks to make the storage contiguous.
Defragment a Linux disk fragment file
Disk fragmentation files rarely occur on Linux systems unless you use a disk with less space or a space that is frequently read and written. Some of the things that can cause Linux disk fragmentation are as follows:
- Edit large video files or RAW images in limited disk space
- Use a disk with very little space on older devices
- More than 85% disk space utilization
- Storing too many files of very small size
The best solution, of course, is to change a larger hard disk, or to manually sort it out.
How to check for disk fragmentation
We can use the fsck command to detect a disk. Remember, be sure to unmount the detected partition when testing, otherwise it may damage the data. Therefore, it is recommended that you perform the fsck command from the Live CD and make a backup of the important files prior to doing so.
When everything is ready, we execute the following command:
- FSCK-FN [/path/to/your/partition]
where [/path/to/your/partition] is the path we want to detect, the detection will have a lot of information output, will eventually be reported to us a disk fragment of the relevant information:
Note: If the disk fragment is above 20%, we need to defragment it. If it is within 20%, it may not be carried out temporarily.
Simple way to organize Linux file systems
Instead of defragmenting the disk, it's not as if it was deleted, and the Linux operation is completely different from the disk arrangement under Windows. Since Linux is a file-based system, we simply copy the files from the disk to another location, format them, and then copy the backed up files back. When copied back, the liunx automatically distributes the files in a continuous sequence.
1. Back up the source disk files using the following command
- CP-AFV [/path/to/Source Disk]/* [/path/to/backup folder]
Note: Many friends will think that large-volume copy files with DD file is better, but the operating level of DD is lower, will copy a lot of junk information. And our goal is to defragment the disk, so use the CP command.
2. Now we use the following command to delete the source file
- sudo rm-rf [/path/to/source disk]/*
Optional: Of course, you can also directly fill the source disk with 0 to achieve the purpose of emptying by the following command.
- sudo dd if=/dev/zero of=[/path/to/source disk]/temp-zero.txt
3. You can use the PV command to monitor the execution of the DD command:
- sudo apt-get install Pvsudo Pv-tpreb | of=of=[/path/to/source disk]/temp-zero.txt
4. Upon completion of execution, we delete this temporary file
- sudo rm [/path/to/Source disk]/temp-zero.txt
5. After disk clear 0 (reliable), we will copy the backed up files back.
- CP-AFV [/path/to/backup folder]/* [/path/to/Source disk]
Using E4defrag
If you think the above steps are too cumbersome, you can also use the E2fsprogs command
1. Installing E2fsprogs
- sudo apt-get install E2fsprogs
2. Perform the Finishing
- sudo e4defrag/
Note: Although the above command is convenient, it is not possible to perform successfully on all systems.
[Go] How to organize Linux disk fragmentation, and the way Windows is very different back to the operating system home