Batch deletion of partitions in Linux

Posted by Dark Training on September 13, 2012 tags: | linux

If you manage a test system with a LARGE volume of disks that you need to clear from time to time, the process of deleting and adding partitions can be a pain.

Here is how to do it in a bash script. In this example I want to delete the partition on each disk

for i in `ls -l /dev/sd[b-k]`; do echo "d
w"|fdisk $i

Boom you now do not have to step through the process of deleting the partitions on each disk. In my case, I'm replicating the commands that I would have run in fdisk had I run it by hand. In this case "D" was for delete, "W" was for write / close. If I wanted to add a partition, I could have used "C" etc.

Hope this helps, if it does feel free to leave a note below for others.