Fragmentation mainly affects big files that will not fit into the free space on a hard disk, so the file is broken up into pieces to fit on it. In other words, its content is scattered all over your hard disk in a somehow chaotic manner. Defragment your hard disk is a good habit, it is the process to reorganize non-contiguos files into contiguos files to optimize your hard drive and increase your PC performance.
❯ sudo umount /media/mydisk2 # Firstly, make sure you umount the partition.
❯ sudo e2fsck -fn /dev/sdb1 # Secondly, we check the partition. The output shows that there are only 0.8 percent of non-contiguos blocks, so it is pretty OK.
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
mydisk2: 1512000/122093568 files (0.8% non-contiguous), 167738935/488369920 blocks
❯ sudo mount /media/mydisk2
❯ sudo e4defrag /dev/sdb1 # Let's defragment the partition. e4defrag needs the file system to be mounted.
❯ sudo e4defrag / # Finally, this is how to defragment all your partitions.