Can I defragment the exfat file system under Linux?

I used rsync to copy files last night, which gave me up to 25% file system fragmentation in my exfat partition.

Is there a utility under Linux to defragment the exfat file system? Or is there a parameter that enables automatic defragmentation?

Hi @Dark_iaji,

From what I could quickly see. no. Please see this for more:

https://unix.stackexchange.com/questions/677611/is-there-a-defragmentation-utility-for-exfat-available-in-gnu-linux-world

Linux native filesystems don’t suffer defragmentation problems. The usual level for 500GB disk/partition is 3%. And for online defragging, you won’t get it any lower as you have to avoid relocating active files.

btrfs has its own internal defrag.

The only filesystems that NEED defragging are from Microsoft - exFAT/VFAT/NTFS. Good luck with those.

And finally, this might help:

https://www.addictivetips.com/ubuntu-linux-tips/defragment-a-hard-drive-on-linux/

1 Like

rsync should not have been the cause of that fragmentation because it creates temporary files by default which are then renamed (moved over) to the original names.
So if you are left with more fragmentation after rsync it only means your current files were already fragmented which caused the spaces to be used by the freshly created temporary files of rsync.

If you are unable to defrag the exfat filesystem at all, i would suggest you to rsync the current contents of that partition to another drive with a different filesystem.
Then delete everything in the exfat partition followed by a sync.
Then when you put the backup back using rsync again they should not be fragmented anymore because they all get created fresh again in the exfat partition :wink:
(This is a kind-of manual defrag but yea works 100%)

This is my alias for my most common arguments i use when using rsync:

alias rsync='sudo rsync -vaAX'

(You could replace sudo with pkexec to get a GUI for the password prompt)

1 Like
sudo e4defrag /dev/sde1
e4defrag 1.46.5 (30-Dec-2021)
Filesystem is not ext4 filesystem

e4defrag only works with ext4.

I’ve tried deleting all files and then copying with rsync, but it still seems to result in 1% disk fragmentation.

Then that partition has bad sectors, because freshly created files on an empty partition should never give any fragmentation by definition, because they are created fresh in one go…

Or you didn’t delete all files… check for hidden files eg names starting with a dot…



I formatted the exfat partition and used rsync to copy the files. Although most of the files are continuous, there are still very few files that are not continuous.

Bad sectors or bad blocks are impossible, because this drive is a qcow2 virtual disk image, and I use a flagship enterprise SSD PM1733 7.68T in my bare metal, and its health is good.

qemu-img check ~/VM/SMI.qcow2 
No errors were found on the image.
3914413/3919616 = 99.87% allocated, 0.09% fragmented, 0.00% compressed clusters
Image end offset: 256574423040

Then I checked my qcow2 mirror.

hmmm that’s quite insane to see :thinking:

I guess you used rsync under Micro$@$ ?
If so i would suggest to try again while using pure Linux, thus not Linux in a VM on Micro$@$, and see if it still happens…

Because the rsync configuration under win is more cumbersome.
So I only use rsync under Linux.
It is not difficult to mount the qcow2 image using qemu-nbd.
As long as the qcow2 disk image is mounted, it can be copied using rsync.