Defragging a USB drive

I often copy files to a USB drive, however these files are often fragmented, and my TV does not like that. It often results in me re-copying files so they are contigious but this is a cumbersome process. I need a utility that can defrag the multiple files so that the MP3 player, or TV can read these files without distortions. What defrag utility can be used for this purpose. I do not have windows, and the devices that use the USB drive needs vfat USB system to read the USB stick with the files being contigious.

A quick search did not find a solution other then copy all the files off the USB, format it, then copy them all back on.

Are you using a USB spinning drive? Prices for USB SSD’s have dropped and they don’t have the same lags when reading fragmented files.

If you defrag the usb stick prior to copying it should copy to contigious regions because the space was made available prior to copy, you can of course do a post copy defrag to make sure. I am uncertain if rsync might be an alternative to look at for more options when copying.
Of course there is dd, but you really have to know your filesystem layouts for using that without causing problems such as data loss. It is a very powerful low level tool.
You may want to install something like Gnome-Commander which is a two pane file manager similar to Norton Commander.

A quick search brings you to Stack Exchange that suggests rsync. Might be worth a try with this. I haven’t verified this works with vFat.

And in case the link stops working for whatever reason.

rsync --preallocate /path/to/source/file /path/to/destination/

rysnc preallocates a contiguous block of storage and copies the file into it. Works for FAT and NTFS too.

Just make sure the file does not already exist at the destination, or rsync won’t reallocate and re-copy it. If it is, delete it, Empty Trash to make sure it’s really gone, then run this command.

Verify if it copied contiguously:

filefrag /path/to/destination/file

“1 extent found” means the file is contiguous. More than one means it’s fragmented.

defragfs is a perl script for linux.

It happens on a normal HDD, USB memory module, or SSD. When files are copied, the player devices cannot read those files or use it effectively

Off course i need that defrag tool to begin with.

I got the utility, copied some random files, and here are the results:

defragfs 1.4.0, Released under GPLv3 (GNU General Public License, Version 3 or any later version) by John Robson, Apr 2016 (help: $ defragfs / -h)

Analysis in progress…


Statistics for /run/media/leon/STORE N GO

Total Files: 37 (1 fragmented file for each 2.84615384615385 files)
Total Fragmented Files: 13 File Fragmentation Rate: 35.1351351351351 %
Total Fragments: 19426 Avg Fragments Per Frag File: 1494.30769230769
10 Most Fragmented Files (for details see: /tmp/fragsfs_runmedialeonSTORENGO-result):

/run/media/leon/STORE N GO/Tiener Week 23’ Teen Week 23’ 9591 extents found
/run/media/leon/STORE N GO/dbeaver-ce-23.3.2-stable.x86_64.rpm: 5906 extents found
/run/media/leon/STORE N GO/dbeaver-ce-23.3.3-stable.x86_64.rpm: 2063 extents found
/run/media/leon/STORE N GO/dbeaver-ce-23.3.3-stable.x86_64(1).rpm: 1601 extents found
/run/media/leon/STORE N GO/Day 4 - Week of Prayer 2024 _ Faith in Every Step - Choosing Well-BQ1FmU_dRq4.mkv: 197 extents found
/run/media/leon/STORE N GO/Invoice492 (1).pdf: 21 extents found
/run/media/leon/STORE N GO/Invoice492.pdf: 19 extents found
/run/media/leon/STORE N GO/Invoice496.pdf: 17 extents found
/run/media/leon/STORE N GO/Naturopathy_-_Module_2.pdf: 9 extents found
/run/media/leon/STORE N GO/What the Bible Says About - Mark Finley.pdf: 6 extents found

You need a defragmentation or you are using -f parameter!

Please specify the percentage of files should be defrag (1-100) [35.1351351351351] or hit Enter.100

Preparing defragmentation, please wait…

Files to be defragmented: 37

You need AT LEAST 2034.726 Megabytes temporarily used for defragmentation (at the directory where you specified), continue (Y/N)? [Y] Y

OK, please drink a cup of tea and wait…

File Number - File Name (Size Mb) [actual extents] - extents after defrag attempt

37 - /run/media/leon/STORE N GO/Tiener Week 23’ Teen Week 23’ (2034.726) [9591] - 1
36 - /run/media/leon/STORE N GO/dbeaver-ce-23.3.2-stable.x86_64.rpm (124.059) [5906] - 1
35 - /run/media/leon/STORE N GO/dbeaver-ce-23.3.3-stable.x86_64.rpm (124.341) [2063] - 1
34 - /run/media/leon/STORE N GO/dbeaver-ce-23.3.3-stable.x86_64(1).rpm (124.341) [1601] - 1
33 - /run/media/leon/STORE N GO/Day 4 - Week of Prayer 2024 _ Faith in Every Step - Choosing Well-BQ1FmU_dRq4.mkv (14.322) [197] - 1
32 - /run/media/leon/STORE N GO/Invoice492 (1).pdf (0.480) [21] - 1
31 - /run/media/leon/STORE N GO/Invoice492.pdf (0.480) [19] - 1
30 - /run/media/leon/STORE N GO/Invoice496.pdf (0.478) [17] - 1
29 - /run/media/leon/STORE N GO/Naturopathy_-_Module_2.pdf (0.486) [9] - 1
28 - /run/media/leon/STORE N GO/What the Bible Says About - Mark Finley.pdf (0.611) [6] - 1
27 - /run/media/leon/STORE N GO/CapitecBankTransactionHistory_11122022-09062023.csv (0.033) [5] - 1
26 - /run/media/leon/STORE N GO/Lara-Croft-Tomb-Raider-(2001)-720P-Bluray-X264-[Moviesfd].torrent (0.063) [2] - 1
25 - /run/media/leon/STORE N GO/CapitecBankTransactionHistory_09062023-06122023.csv (0.047) [2] - 1

Defragmented Files: 13 / Fragments: 19426


Analysis in progress…

.The selected directory contains no framented file!