How Disk Defragmentation Works
A 2D view of the cluster
1. When you freshly install your OS, your files are arranged nicely, starting at the beginning of the drive. The blue and orange areas are occupied. The grey area is free space.
2. When you add a file (represented by the green space above) the OS places it wherever it finds space. The OS could have placed it contiguously, but it didn’t.
Fragmentation
3. Over time, this is what your hard disk begins to look like, with files scattered all over the place, placed non-contiguously.
1. We have a blue file and an orange file. Both are fragmented. The first part of the orange file has data in it that points it to the next part, and so on. The OS can thus figure out how to access the entire file.
2. When you begin defragmenting the drive, the defrag program calls the filesystem API (Application Program Interface) to move the first part of the orange file to a different location. It makes a best effort to place it such that the orange file will eventually become contiguous.
Defragmentation
3. The OS knows which part of the orange file belongs where, as mentioned in step 1. Eventually, all of the orange file is accessed and placed contiguously. The same happens with the blue file. There needs to be enough free space for this to happen successfully.
The earlier example showed what happens with, say, Windows defragmenter. Better defragmenters such as PerfectDisk would arrange the orange and blue files this way, beginning at the start of the drive-so they can be accessed faster.