How To Defrag in Linux

If your computer uses a hard drive with a spinning disk (like about 99% of computers), your data will probably be scattered all around the disks. Hard drives work with an index or table of contents, where each particular piece of a file is physically located. When a file needs to be accessed, the hard drive head will move to these blocks of physical space on your hard drive to read or write information.

Unless you use a solid-state device for your storage (such as a flash drive, or a true SSD, which comes with some expensive portable computers) data that is extremely fragmented will cause slowdowns in accessing information. On computers running Windows, defragmenting is usually a no-brainer, as Windows usually schedules defragmenting at regular intervals. With Linux becoming a popular choice for computers nowadays (especially netbooks, laptops, and PCs in educational settings), more and more users are trying to learn how to do things on Linux.

Some new (and old) Linux users might be asking how to defragment their hard drives. The usual answer that experts will give you is that you don’t necessarily have to defragment your hard drive when you’re running Linux. This is because Linux—as well as other UNIX-based operating systems like the Mac OS X—organize information more efficiently with their Journalized file systems (either EXT2, EXT3 or HFS). Users running Windows, meanwhile, will not commonly experience performance hits until the fragmentation of their hard drives reach about 20%. This is often a rare scenario, even on Windows. And on Linux, the fragmentation level is often below 1% for normal users.

Still, regular defragmentation can be a solution if you feel that your computer’s performance is slowing down markedly. Here are a few ways you can defrag using Linux.

Defrag from Windows

You can download tools that will let you defragment a Linux file system on Windows. This can be particularly useful if you are dual-booting Windows and Linux. Look for the driver “EX2 File System for Windows.” This lets you access EXT2 and EXT3 file systems right from Windows. The driver also enables you to defragment the file system right from within Windows.

Defrag from Linux

There are a handful of ways you can install applications on Linux, depending on the flavor or distribution of Linux that you use. The most popular these days, though, is Ubuntu, which is a fork of the Debian distribution. Therefore, this will use the APT method of installing applications straight from online repositories.

You will need to install the application called “defrag.” Open a console window and enter these commands.

sudo apt-get update


sudo apt-get install defrag

“Sudo apt-get update” will update your system’s list of repositories, to make sure they are current.

“Sudo apt-get install defrag” will download the defrag application from the best repository. Remember that “sudo” commands mean you are running the application as a super user or administrator. You will be asked to enter your administrator password if your computer is set up with one. To learn the command-line options of the defrag program, enter the following command.

man defrag

This will give you command line options and other information about defragmenting your Linux file system.


Share this article!

Follow us!

Find more helpful articles: