lsblk + how to capture the disks that are without filesystem

Dear friends and colleges

we have servers with rhel 7.5 version , and each server include disks with filesystem and disks without filesystem

usually sda – is the OS disk , and sdb is disk that already have filesystem,

with the following lsblk syntax we can capture the disks list with filesystem type

lsblk --fs  -o NAME,FSTYPE
NAME             FSTYPE
├─sda1           xfs
└─sda2           LVM2_member
  ├─VG-LV_root xfs
  ├─VG-LV_swap swap
  └─VG-LV_var  xfs
sdb              ext4

I want to know how to match all the disks that are without filesystem

expected output


so we try the following syntax

lsblk --fs  -o NAME,FSTYPE |  awk '$2 == "" {print $1}'

but we get the following


so we get sda in spite sda have filesystem ( XFS )

so how to capture only the real disks that are without filesystemsdc / sdd

Asked By: user436442


If your lsblk supports the --json output format, and if you have jq installed, you could parse that for block devices that (a) have no children (i.e. are unpartitioned) and (b) have no defined filesystem themselves:

lsblk --fs --json |
  jq -r '.blockdevices[] | select(.children == null and .fstype == null) | .name'
Answered By: steeldriver
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.