How do I reformat a USB stick containing read-only files?

Hi. I am running 32 bit Ubuntu 18.04.2 LTS, Gnome 3.28.2. I have inherited a 2GB USB drive which has 2 partitions. One of those partitions has a folder which is locked (there is no physical switch present) and read-only. I wish to reformat the whole drive with FAT32 and erase all the files present. How do I do that please? I have looked on the net in various places but I have not had any success as yet. Thanks in advance.
Andrew

1 Like

I would try using GParted. It should allow you to wipe all partitions and reformat the whole drive.

5 Likes

Gparted has always worked for me also.

3 Likes

Thanks Guys for your responses. I have tried GParted this afternoon and unfortunately it did not work. The problem seems to be the read-only permissions which I can’t alter.
I tried ‘sudo chmod 666 /dev/ sdY’ where ‘sdY’ is the device assignment for the USB drive, which is supposed to allow R & W peermissions but that did not work either. Any further ideas?

1 Like

your drive should be listed in /etc/mtab and that should give you an idea if it is mounted read-only (ro). if not there might just be an issue with the drive.

if it is read-only, you could try remounting it like in @Akito’s description below. or there is more info about remounting as read/write in the askubuntu link below that.


2 Likes

You need to make sure the drive hasn’t been mounted before trying to delete any partitions. If it has been mounted, then open a terminal to unmount it (sudo umount /{path to mounted partition(s)}.

When none of the partitions on the drive are mounted, GParted should allow you to delete them and reformat the space as you please.

1 Like

Try this one:
Rightclick the usb-stick and unmount.
Go the the terminal and launch it, type:
sudo chown -R username:username /mnt/your stick name and Enter
Reformat the stick/partition

Or try at the command line (make sure you’re using the correct device!).

e.g. sudo fdisk -l (to list connected mass storage devices - and their partitions).

Let’s assume your “/” - main Linux O/S disk is on /dev/sda (and /dev/sda1, 2 etc).

So the USB you want to nuke might be /dev/sdb (don’t take my word for it though! VERIFY! And after verifying, VERIFY again :smiley: )

sudo fdisk /dev/sdb - and delete the partition(s).

sample session (i.e. first off list drives and partitions):

sudo fdisk -l

(below is the one I’m “wiping” the partition off) :

Disk /dev/sdb: 7.2 GiB, 7731314688 bytes, 15100224 sectors
Disk model: Trans-It Drive  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x22fb6a74

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1  *     2048 15099903 15097856  7.2G  7 HPFS/NTFS/exFAT

╰─➤  sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

– (“p” will print partitions)

Command (m for help): p
Disk /dev/sdb: 7.2 GiB, 7731314688 bytes, 15100224 sectors
Disk model: Trans-It Drive  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x22fb6a74

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1  *     2048 15099903 15097856  7.2G  7 HPFS/NTFS/exFAT

— (“d” will delete partitions - in this case there’s only one so I pressed “enter” for default of “1” - if you’ve got several, you might have to select 2, or 3 etc)

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sdb: 7.2 GiB, 7731314688 bytes, 15100224 sectors
Disk model: Trans-It Drive  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x22fb6a74

— (then I pressed “w” to write the changes - then “q” to quit - don’t worry about the message “Failed to remove partition” - it’s lying to you :smiley: - this probably happened because the stick got automounted when I inserted it) :

Command (m for help): w
The partition table has been altered.
Failed to remove partition 1 from system: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.

sudo fdisk -l

(snip snip)

Disk /dev/sdb: 7.2 GiB, 7731314688 bytes, 15100224 sectors
Disk model: Trans-It Drive  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x22fb6a74

(note - there’s no longer “/dev/sdb1 * 2048 15099903 15097856 7.2G 7 HPFS/NTFS/exFAT” showing)

Now it would probably be a good idea to eject / safely remove the device… plug it in again and run “sudo fdisk -l”…

Now you could run fdisk again on it and create a new partition - or - use gparted (probably easier - but not for me - I find the CLI infiinitely easier than GUI).

I went back and created a new Linux (83) partition with “defaults” - i.e. : in “sudo fdisk /dev/sdb” - I just pressed “n” for new, then enter, enter, enter (to use the whole device with defaults) then “w” to write changes… Then I also ejected it again, and re-inserted it…

Then :

mkfs.ext4 /dev/sdb1

╰─➤  sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7.2 GiB, 7731314688 bytes, 15100224 sectors
Disk model: Trans-It Drive  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x22fb6a74

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 15100223 15098176  7.2G 83 Linux

Never be complacent - always verify… I do this stuff all the time, almost with my eyes closed, when provisioning Linux servers - however - I still do make mistakes occasionally and fdisk the wrong device, but thankfully I’ve never done it on a production server (I sometimes get complacent when I’m doing stuff on a home server)…

2 Likes

Too many suggestions already give for you.
As last alternative: Try to format in a PC with Windows.
Any a can solve. Win 7, 8, or 10. :exploding_head:

1 Like

Again, thank you to you all for suggestions to try and solve this difficulty. At the moment, I am learning how to use the terminal and navigate my way through the file heirarchy. I started using Dan Tripp’s suggestion because I was imitating his typing; however after entering the command: w enter, a message appeared:
fdisk: failed to write disk label:operation not permitted.
Disk label type is: dos
And so I could not get any further with that one.
cordx suggested seeing if my drive was in /etc/mtab and if it was mounted read-only. What do I type in the terminal to do that please?
Am I right in thinking for gparted to delete a partition the drive needs to be unmounted?
Will try the other methods and report back. Thanks again
Andrew

1 Like

cat /etc/mtab will print out the contents of your mtab in the terminal. there will be a lot of info printed out. my three usb’s are at the very bottom so hopefully the drive you are looking for will be as well.

for example, my 4 gb usb is listed as:

/dev/sdcX /media/myusername/vol_work vfat rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0

the part you are interested in is just after the filesystem type (vfat in this case) where mine says rw (read/write), you are looking to see if yours says ro (read only).

I can only think that you didn’t use sudo… make sure nobody (e.g. you) doesn’t have a file manager (e.g. nautilus) session looking at the mounted drive, or - a shell session in the mount point directory (check all your terminal sessions, close any you don’t need, make sure you’re in “~” - i.e. $HOME - “cd ~”).

sudo fdisk /dev/[device]” shouldn’t care whether it’s mounted, but might barf if a user has a “session” looking at the mount point…

I don’t usually use youtube for software tech support or recommend it - but his might help you :

1 Like

Hi Guys: Thanks for the suggestions. Here is some promised feedback of how I am getting on. I ‘cat /etc/mtab’ my usb drive and yes it is ro. I then used the method in the youtube video Daniel suggested and it indicated it had changed the ro to rw. However when coming to format the disk, the files were still present.
While using GParted on the usb disk, it was written the File system was fat 16, status : unmounted; Path: /dev/sdd1
Warning
Cant’t open /dev/sd11: No such file or directory
Cannot initialise ‘::’
label: Cannot initialise drive
Can’t open /devsdd1: No such file or directory
Cannot initialise ‘::’
fsck.fat4.1 (2017-01-24)
open: No such file or directory
Unable to read the contents of this filesystem!
Because of this some operations may be unavailable.
The cause might be missing software package.
The following list of software packages is required for fat 16 filesystem support: dosfstools, mtools
Does this suggest I need to install added software to read the filesystem before it can be altered?
I have also tried using Windows to format the usb but it did not.
Will keep trying the above suggestions.
Andrew

What do you mean? After formatting?

Try:
WARNING: the -f option allows usage on a mounted drive, so be absolutely sure you select the correct drive.
You can remove the f option, if you manage to dismount the target drive.

sudo badblocks -svf /dev/sdd -o badblocks1.log

You also may add the w option, since you don’t care to keep the files on the drive. Adding the w option usually overwrites all files on the target drive.

After I tried formatting the USB drive with GParted, I got the warning message above in my previous post which to me indicated the drive had not been formatted.
I then checked to see if the files in the locked folder had gone; but they had not.
It seems to me because I did not create the locked folder I need to either change the ownership or override the write protection. I will continue trying the above suggestions. Thanks again for your help.
Andrew

Another phenomenon that could explain this is that you try to mount the stick with the wrong file system. E.g. I once mounted a ZFS enabled disk as NTFS and it showed some old data from before I enabled ZFS on it. The data was, as expected, still on the HDD, but it just became visible after deletion because of using the wrong file system when mounting the medium.

A post was split to a new topic: How to enable read-write permissions on a USB flash drive