0

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)

Sandeep Kanabar
  • 1,264
  • 17
  • 33

1 Answers1

3

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 } }'
Sandeep Kanabar
  • 1,264
  • 17
  • 33
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • `lsblk --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { if ($1 != "" ) dsk=$1;if ( $2 == "") print dsk }'` gives me the following: `sda └─sda1 sdb sdc sdd`. I only want the unformatted disks i.e. sdc and sdd: – Sandeep Kanabar May 10 '17 at 10:52
  • 1
    OK try: lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==0) print i } }' – Raman Sailopal May 10 '17 at 11:32
  • Excellent. Worked like a charm. Just had to make a small change. This is what worked: `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 } }'` Instead of `if (dsks[i]==0)`, `if (dsks[i]==1)` worked. – Sandeep Kanabar May 10 '17 at 12:53
  • Can you please update your answer so that I can accept it. – Sandeep Kanabar May 10 '17 at 13:21