2

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?

1
  • 3
    isn't unecessay excessive writes on flash media considered a bad idea?
    – Rony
    May 16, 2012 at 5:27

3 Answers 3

7

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

6
  • Never knew that about Solid State Drives...You learn something new everyday! :) And I suppose that would prevent fragmentation. Great answer Sergey! May 16, 2012 at 5:29
  • 1
    Fragmentation affects performance of rotational drives because the reading head has to physically move from one location to another in order to read a fragmented file. There are no moving heads in a SSD, that's why they're called "solid-state drives". But, say, if you have a floppy :) - removing all files from it returns it to its pristine un-fragmented state.
    – Sergey
    May 16, 2012 at 5:35
  • I'm a novice to a lot of hardware-related things. I understand how data is read from a drive with a stylus, such as a HDD. But, as for SSDs, how do computers read information from them? How is it stored? I would look it up but you seem to know a great deal about this kind of stuff :) May 16, 2012 at 5:37
  • Sergey, sorry, you are wrong. For example to boot an OS off of a solid state drive the ISO file has to be defragmented. I have a solid state drive with several ISO-s and they need to be in one contiguous area of the file system. @RyanMcClure You should read Wikipedia pages on those topics and some related topics (going by the links in first articles) first and then youll be able to ask questions (which you should first search for online and read up and then ask any questions you couldnt easily find an answer to.
    – Carolus
    Sep 13, 2016 at 11:43
  • 1
    @Carolus: Nowhere did the OP mention he's going to boot multiple OSes off the flash drive, which is also not something many people ever need. Yet if he really needed to defragment a drive then the first paragraph of my answer provides instructions how to do that without any special tools.
    – Sergey
    Sep 14, 2016 at 2:45
2

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

1
  • I never considered the possibility of using virtualization for it. Great idea! May 16, 2012 at 5:29
0

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:

fdisk -l

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

2
  • 1 ungodly reason why a lot of useful and even common tools arent on the Ubuntu disk, is because theyre trying to keep the filesize low.
    – Carolus
    Sep 14, 2016 at 10:31
  • 1
    Well, such drive tools take up very little space because they reduce to just a few lines of machine language. Ever look at the size of the disk defragmenter program in windows? Not exactly bloatware. Even if a function like defragment were coded into an existing app like gparted, which seems like the logical way to add that functionality, it wouldn't hardly make a dent in the size if the overall app. LOL The real reason modern linux don't even bother with defragment is because the file system used is a journaling file system which by definition shouldn't ever need defragmenting.
    – user28788
    Jun 13, 2017 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .