Skip to content
Advertisement

Shell script to list unformatted disks (partitions)

I’m looking to create a script that lists the unformatted partitions/disks in Linux OS.

I couldn’t find a reliable way of listing the unformatted disks / partitions.

I tried to use lsblk --output NAME,FSTYPE,MOUNTPOINT | grep "sd" which outputted:

sda
└─sda1 xfs    /
sdb
└─sdb1 ext4   /mnt/resource
sdc
sdd

The names which do not have mountpoint listed against them are unformatted disks. How can I get those names? Or what would be the best way to get these?

I’m on CentOS Linux release 7.3.1611 (Core)

Advertisement

Answer

Try something like:

lsblk --output NAME,MOUNTPOINT | awk -F / '/sd/ { if ($1 != "" ) dsk=$1;if ( $2 == "") print dsk }'

We are storing the partition above the mountpoint line in a variable dsk and then printing it out if there is no mount point i.e. $2 equals null.

Pasting the answer that worked from the comment:

lsblk -r --output NAME,MOUNTPOINT | awk -F / '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'
Advertisement