Let’s be direct about this, you cannot defrag your Mac. You actually don’t need to do this since Mac’s built-in operating system utilities already ensures your Mac files don’t get fragmented. Leave it to the Mac support guys to think about everything. Specifically, the OS X and later operating systems have this defragmentation utility already built in.
If you want to still be proactive about maintaining your Mac, you can go into Disk Utility to make sure your hard drive and files are not damaged. Further, you should make sure you have plenty of free space on your hard drive. You’ll need more than 10% free in order for the Apple defragmentation process takes place in the background.
If you feel that your Mac is running slowly, it is most likely not because your hard drive is fragmented. You probably have a failing hard drive, corrupted system or program, or another Apple computer issue.