Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have just installed Ubuntu 11.10 on the Btrfs filesystem and I want to ask a question about defragmentation of files.

Do I really need to defragment files or the whole system?

Defragment # btrfs filesystem defragment /pool1 Defragment

Btrfs defragmentation

share|improve this question
No, btrs, ext4,3,2, ETC... don't require you to defragment the drives, they do suffer from it as any other filesystem does. – Uri Herrera Dec 1 '11 at 4:20
But nowhere near the same as NTFS. – Uri Herrera Dec 1 '11 at 4:21
updated......... – One Zero Dec 1 '11 at 8:20
up vote 6 down vote accepted

You don't really need to defrag Btrfs filesystems, just like you don't really need to defrag Ext2/3/4 filesystems.

Yes, Btrfs is COW (copy-on-write), which would imply it fragments files much more than Ext, but this is addressed in several aspects of the design, including the ability to easily defrag the filesystem while it is online. This excerpt provides more detail:

Automatic defragmentation

COW (copy-on-write) filesystems have many advantages, but they also have some disadvantages, for example fragmentation. Btrfs lays out the data sequentially when files are written to the disk for first time, but a COW design implies that any subsequent modification to the file must not be written on top of the old data, but be placed in a free block, which will cause fragmentation (RPM databases are a common case of this problem). Additionally, it suffers the fragmentation problems common to all filesystems.

Btrfs already offers alternatives to fight this problem: First, it supports online defragmentation using the command btrfs filesystem defragment. Second, it has a mount option, -o nodatacow, that disables COW for data. Now btrfs adds a third option, the -o autodefrag mount option. This mechanism detects small random writes into files and queues them up for an automatic defrag process, so the filesystem will defragment itself while it's used. It isn't suited to virtualization or big database workloads yet, but works well for smaller files such as rpm, SQLite or bdb databases.

So, as long as you don't plan to run IO-intensive software like a database under significant load, you should be all good.

To check the fragmentation of files, you can use the filefrag utility:

$ find /path | xargs filefrag > frag.list
# Now you can use your favourite tools to sort the data

On Systemd systems, /var/log/journal/ will probably be the most fragmented. You can also look at ~/.mozilla and other browsers databases.

To defragment, use:

$ sudo btrfs fi defrag -r /path

Note that, as you probably know, the current (reference) Btrfs implementation is not completely stable yet, so you shouldn't use it for critical data or heavy loads (using it on a production server, for example, is probably not the wisest thing to do); Fedora 16 is using it by default (or will be), so it seems to have reached a certain maturity.

share|improve this answer
how do i know its defrag.... or not ...command to check defrag status – One Zero Dec 3 '11 at 16:54
This answer is wrong as per the BTRFS wiki: btrfs gotchas. The problem of file fragmentation can easily be reproduced by running systemd-journald on a btrfs volume. Although I have not tested yet inhowfar the autodefrag option solves this problem. – Jonas Wielicki Nov 27 '13 at 16:32
I've been using btrfs full-time now for at least 18 months, with systemd, firefox, etc. A quick check using filefrag reveals that journal files are indeed heavily fragmented (averaging 10k extents each), but I have not observed CPU spiking nor performance impact. I do not have autodefrag on, will enable it and report back at next reboot. – Félix Saparelli Nov 28 '13 at 10:38
So: file fragmentation does occur, autodefrag does help, performance impact is minor at best on everyday systems. Obviously heavy-load servers might need to measure and tune things up, but then again this is alpha software so you might want to avoid it there anyway. – Félix Saparelli Dec 7 '13 at 12:19

For the the sake of this topic i think is better to clarify that:

Any filesystem != recovery/management utilities

Keep in mind that the filesystem is about how data is organized on a physical hardware, every other activity is done with extra software and utilities that, especially in the GNU/linux, are done by peoples that probably are not strictly related with who had made the filesystem.

This is probably not the case of the Btrfs, but sometimes we are near to that because desktop and enterprise environments can provide different solutions with different grades of realiability.

The answer to your question is "no" but is a "no" related to the normal behaviour of a generic filesystem, and every utility you can find about this is simply a different project.

When you need to defrag your filesystem keep in mind that you are going to use external software that is not really related to the life of the filesystem itself, with all the pros and cons.

share|improve this answer
updated........ – One Zero Dec 3 '11 at 8:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.