lsblk + capture only the disks

9,143

Solution 1

If you want only disks identified as SCSI by the device major number 8, without device partitions, you could search on device major rather than the string "disk":

lsblk -d | awk '/ 8:/'

where the -d (or --no-deps) option indicates to not include device partitions.

For reasonably recent linux systems, the simpler

lsblk -I 8 -d

should suffice, as noted by user Nick.

Solution 2

I wanted to get only the device names of all disks without any other output. Ended up using this:

lsblk -nd --output NAME

Which yields something like

sda
sdb

-d only outputs disks, -n removes the header line, and --output NAME make sure only the name of the device is listed.

Share:
9,143
yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael almost 2 years

    I want to capture only the disks from lsblk

    as showing here fd0 also appears in spite its not really disk for use

    in this case we can just do lsblk | grep disk | grep -v fd0

    but maybe we missed some other devices that need to filter them by grep -v

    what other disk devices that could be appears from lsblk | grep disk and not really disks ?

    lsblk | grep disk 
    
    fd0                2:0    1     4K  0 disk
    sda                8:0    0   100G  0 disk
    sdb                8:16   0     2G  0 disk /Kol
    sdc                8:32   0     2G  0 disk
    sdd                8:48   0     2G  0 disk
    sde                8:64   0     2G  0 disk
    sdf                8:80   0     2G  0 disk
    
    
    lsblk
    NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    fd0                2:0    1     4K  0 disk
    sda                8:0    0   150G  0 disk
    ├─sda1             8:1    0   500M  0 part /boot
    └─sda2             8:2    0 149.5G  0 part
    ├─vg00-yv_root 253:0    0  19.6G  0 lvm  /
    ├─vg00-yv_swap 253:1    0  15.6G  0 lvm  [SWAP]
    └─vg00-yv_var  253:2    0   100G  0 lvm  /var
    sdb                8:16   0     2G  0 disk /Kol
    sdc                8:32   0     2G  0 disk
    sdd                8:48   0     2G  0 disk
    sde                8:64   0     2G  0 disk
    sdf                8:80   0     2G  0 disk
    sr0               11:0    1  1024M  0 rom
    
    • Admin
      Admin over 6 years
      fd0 is a floppy disk, so it is a "disk". There may be nothing in it, but the device exists. Ditto for sr0. There may be SCSI or SATA disks that are not mounted/ Are they to be included or not? IOW, the selection criteria have to be specified a bit more precisely.
  • NickD
    NickD over 6 years
    lsblk -I 8 -d works too.
  • user4556274
    user4556274 over 6 years
    @Nick: if -I 8 is supported. Looks like for GNU systems, it was introduced somewhere between coreutils 8.21 and 8.25.