I have a flash drive that I use for general use around the house and at school. I use exclusively Ubuntu, but at school and on other family computers, they use Winblows. Therefore, my flash drive is in FAT32 format for easy reading on both types of machines. However, I want to be sure that my drive can be defragmented. Is there any way to do this on Ubuntu?
Moving all the data off the USB drive and then copying it back again will ensure that there are no fragmented files.
However, there's absolutely no point in doing this since fragmentation does not affect performance of solid state drives
If your computer is faster enough , e.g has cpu VT support
Launch a Windows virtual machine to do the defragment stuff for your USB key , e.g VBOX
One reason to defrag a usb drive is if it will be used in your car stereo as a music source. For example, if you add a folder to your drive, your stereo might not be smart enough to place it alphabetically in line with the older folders. If you add tracks to a folder, you might find the tracks missing or somewhere near the last folder on the drive when searching through the drive on the stereo. A computer reads drives far more intelligently than a car stereo. A car stereo that reads usb drives has a very basic computer protocol and a fragmented drive will literally confuse it. I know this to be the case on the two different Sony stereos I have in my 2 cars.
Copying the contents to a computer, deleting the files from the drive, and returning them back to the drive from the computer won't work properly either. You end up with DUPLICATE folders! This is because the little mark the computer places on your drive that says "these files are deleted" is completely ignored by a car stereo. The files are still there, your computer just is smart enough to ignore them if you tell it to delete them. A car stereo is much much dumber.
Now, theoretically, if you were to replace 8 gb of data with 8 gb of data, it should overwrite the same sectors with that data and no errors should pop up. But if you added a folder and your new folder was to be placed somewhere in the middle alphabetically, then you would offset the remaining folders after that folder in a weird way. Replacing 8 gb of data with 8.2 gb of data with a folder in the middle would mean the 4 gb that came after that new folder would occupy slightly different parts of the usb drive. You might end up with the last 0.2 gb of the drive duplicating folders. Likewise, if you removed a folder, and did the copy, delete, and copy back procedure, you might end up with a few folders at the end of the list duplicated.
The only way I have found around this is to format the USB drive. In windows, you would opt to NOT do the 'quick format' as that just places a little note at the beginning of the drive without changing any of the data. The drive looks empty on the computer, but your car stereo will still think files are on it or show an error. So use the full length format procedure that takes a while (because it is doing stuff to the whole drive). That actually wipes the drive so you can start clean.
In ubuntu there are a few ways to get a fresh USB. gparted is the easy graphical tool, though it also shows your bootable drives and you can REALLY screw up your computer if you don't look carefully at what drive is being represented and select the right drive for the format process. A usb should be sdb1 or something like that as opposed to sda1 which would be your primary harddrive. Don't screw with anything with 'sda' in the name and make sure what you DO screw with is the same size as your USB drive and disappears if you remove the drive. (you have to refresh to see the change in gparted).
The command procedure for wiping a usb drive (after you have copied everything off of it) is pretty easy, actually.
You want to be sudo so I just do a "sudo su" as my first command and enter my password. First, you will need a few apps that don't come preinstalled with ubuntu (for some ungodly reason). Type the following:
apt-get install dosfstools
This will install the necessessary stuff to format to fat32 and do the magic. If you already have it, nothing will change.
Then do the following command:
you will see a list of your drives, including the internal ones and the usb. The usb will probably be sdb1 or something with "sdb" and a number. You can double check (smart) by removing the usb drive and just repeating the command. If the sdb drive disappears from the second list you know that is the right one. Reinsert your drive and do fdisk -l one more time to be sure it reappears in the same place with the same sdb name. I will use "sdbx" in these commands, but you need to substitute the correct drive name instead, be it sdb1 or sdc1 or whatever your fdisk -l list shows.
Next, you type the command
mkdosfs -F 32 -I /dev/sdbx
For some weird reason this is really really fast and you might think it failed. But you will see no error if you did it right. You might have to unmount the drive and retry the format command again. If you get an error talking about unmounting the drive or that it is busy, close any windows showing the contents of the drive and type
sudo umount /dev/sdbx
I swear someone way back in the day screwed up the coding such that unmounting is done with umount istead of unmount. A simple mispelling and it will now and forever be that way. LOL
Once you have unmounted with umount, just do the format code again. It should work fine this time. Once you have done this code, you can close the terminal (when it is done) and copy your stuff back to the usb drive. I am not sure why it is so fast in ubuntu but so slow in windows. It almost seems like ubuntu is doing the same thing as a 'quick format' in windows, but I know for a FACT quick format in windows does NOT work, whereas this fast format in ubuntu ALWAYS works.
Anyhow, hope this helps someone