Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the best way to defragment a FAT filesystem when running Linux/Unix (on usb stick for instance)?

share|improve this question
3  
I would go for your first option if space isn't an issue. I would create a clean filesystem in a large file that is loop-mounted. That way you get the opportunity to check the result before overwriting the stick. –  jippie Mar 16 '13 at 21:24
    
@jippie's suggestion also offers a backup. Else I'd go for a defrag utility, but check carefully first it is reliable, and back up beforehand if I have any attachment to the data. –  vonbrand Mar 16 '13 at 21:40
    
@jippie : you mean creating a loop file the same size of the initial fat partition, copy all to it, check, then dd to the original device? You will need extra space, and I don't see how it's better from just simply copying the file to the local ext filesystem temporarily. Or I'm missing something. –  Marc MAURICE Mar 17 '13 at 7:47
1  
You can check the result before changing the content of the flash drive. –  jippie Mar 17 '13 at 7:49
add comment

1 Answer

You can check whether a file is fragmented or not using the filefrag utility.

That way you could filter out the files that aren't fragmented, and do the copy/copy back only for already fragmented files. That should save you some time. However be aware that there is no guarantee the new file layout will be better than the old one.

The best way is to ignore file fragmentation altogether. Especially on a USB stick it hardly matters, only wastes write cycles, and unlike HDDs, there are no movable parts and thus not much of a penalty due to fragmentation.

Defragmentation should only be necessary if something funny happened that caused files to fragment in a very extreme way. For example torrent clients that download file contents in random order without preallocating. But unless FAT started supporting sparse files at some point that's not even an issue there, as preallocating is mandatory on such a FS.

share|improve this answer
    
Thanks for your answer. In fact I need a way to fully defragment the FAT filesystem because I'm using Easy2boot, and every iso file must not be fragmented. –  Marc MAURICE Mar 24 '13 at 23:13
    
Sounds like an outdated method to me. GRUB2 isoloop is very popular, so lots of distros support loop-mounting an iso from anywhere already. Those that don't, it may be that it works directly from a partition, but it might just as well fail. Entirely depends on the ISO itself. –  frostschutz Mar 25 '13 at 2:32
2  
The drawback with GRUB2 isoloop is that it will directly load a Linux kernel in the ISO. You have to know the path of the kernel/initrd to load, and you will loose the ISOLINUX menu offering custom boot options. –  Marc MAURICE Apr 2 '13 at 11:35
    
Finding the path isn't hard, and the rest depends on how much effort you want to make to convert the isolinux menu items to a grub2 submenu... but yes, all solutions have their drawbacks. I just tested my ISO partition - NONE of the files are unfragmented. And they were copied one by one. Fixing that would be a major pain for me, so I'll rather forgo on ISOs that do not support isoloop mounting. –  frostschutz Apr 4 '13 at 10:29
    
I never used easy2boot myself, but since this solution involves the creating of a partition, couldn't you do that in the first place - one partition per ISO, and dd. No fragmentation possible this way. Or alternatively if that's possible at all, a fragmentation-free filesystem (ISO inside ISO?) –  frostschutz Apr 4 '13 at 10:32
show 1 more comment

Your Answer

 
discard

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.