I am wondering how to wipe an SD card using my Raspberry Pi. The card I am hoping to wipe has had pictures on it from a camera, and I have deleted these. All that remains is the undeletable folder named 'DCIM.' I do not wish to plague my parents with this issue, so is there any way I can wipe this card with my Raspberry Pi? Many thanks to anyone willing to help.
Using a USB sd card reader, you can.
Using a tool called "fdisk"
fdisk -lto identify the correct drive.
Run the following in case the SD card is
dto remove the partition
nfor a new partition and basically follow the prompts.
More info here
If you have a card reader you can plug it into the USB port and reformat the card using the directions in this question. Note the link I provided gives several alternative methods of formatting an SD card including using a digital camera (not all have this feature).
The link that Steve and Havnar gave have good information, but I have seen where fdisk can get 'confused' if the partition table itself is corrupted, or if a 3rd party partition manager was used on the card (although admittedly, those aren't nearly as common nowadays as they once were).
There is another way of wiping the card completely - including the partition table - albeit it can take a long time. From the terminal session, type these commands:
sudo su -
[[ then enter your password at the prompt ]] this will allow you to become the 'root' user or superuser in linux. You'll need to be root to run the next command, and for the tutorial I'll assume that you have already ascertained that the SD card you want to wipe is
dd bs=512 if=/dev/zero of=/dev/sdb
dd command is a very low-level command in Linux and can basically copy anything to any device. The
bs parameter is short for 'byte size' and 512 is usually the sector size for SD cards.
if= specifies the 'input file' but this description is a bit misleading as it can be any named pipe, device, etc. It's not limited to files. In this case, you're pulling input from a device that does nothing but return zeroes whenever requested.
of= is 'Output File" and again, you're not writing to a file per se, but writing to the raw disk device. So, what this command does is write zeroes to every sector on the card, thereby wiping it.
This command will take a very long time to complete, but when it's done your card will be completely blank. I should also warn you, that the command by default has no output until it has completed, so it might look like it's not doing anything, but it is. More on this below.
Now, if you did have a corrupted partition table but you're not worried about wiping every single sector, usually the first 10k-100k sectors are sufficient to wipe, then repartition the card with
fdisk. To only wipe the beginning part of the card, add a parameter to the
dd bs=512 count=100000 if=/dev/zero of=/dev/sdb
count= parameter will only copy zeroes to that number of sectors and then will quit. This will be much faster than wiping the full SD card.
As an aside, the
dd command is also useful for writing disk images to the SD card. Let's say you just downloaded a newer version of the NOOBS package for your Raspberry Pi. In linux, you can transfer this image to an SD card by this command:
dd if=/home/userdir/Downloads/noobspackagefile.img of=/dev/sdb
Again, assuming that the card you want to write to is mapped as /dev/sdb, and set the directory & name of the downloaded NOOBS image correctly in your
if= statement, that will transfer the file sector-by-sector to the SD card that you can then set as your boot device on your Raspberry Pi and boot from it.
Lastly, if you are fully wiping a device in this manner or transferring a large image, there is a way of seeing how far along the process is. In another terminal window, become the superuser again (as outlined above) and type this command:
ps -ax | grep dd
you may get more than one line of output, find the correct line for your
dd command and note the number directly on the left. That's the Process ID or
pid of your running
dd command. Now, type this command:
kill -USR1 #####
where ##### is the Process ID noted above. This will interrupt the
dd process just long enough to allow it to output how many bytes/sectors have been transferred since the command was invoked.
This works for any linux,
wipefs /dev/sdcard(replace sdcard with the name of the device) and note the partition table (if any) type and partition type(s)
wipefs -a /dev/sdcardto wipe the partition table (and partitions)
- you can then either:
a) recreate the partition table and partition(s) with a tool such as
gparted(runs in X)
fswith the desired filesystem, I recommend
vfatfor windows and
Of course, this requires one to have the
mkfs utilities and
wipefs. I believe both tools are included by default on most distributions. If
mkfs.vfat doesn't exist, install it with:
sudo apt-get install dosfstools on Debian (or Raspbian or any other Debian derivative)
pacman -S dosfstools on Archlinux (as root)