2

I need to filter the result of this command:

lsblk -rno "name,size,mountpoint,fstype" | grep -v 'sda'

Let me give a result of the above command on my computer:

sdb 223,6G
sdb1 179,8G /VMs/qemu ext4
sdb2 29,5G [SWAP] swap
sdc 15G  ntfs
sdd 7,5G
sdd1 7,5G  ext4

The filtering I need is: Only the ones which are NOT mounted and the ones which have file system to be left. So ideally what would be left after filtering should be:

sdc 15G  ntfs
sdd1 7,5G  ext4

My goal is to have a script (which can be called using rofi of dmenu) to mount usb drives. I've fould solutions on the net, but the problem with these is that they assume that all drives have partitions. As you see, I use external drives formatted without being partitioned first (the ntfs for example). So I need this change.

2
  • You are already filtering the output of lsblk with grep. It sounds like what you want is "a list of partitions, other than for /dev/sda, that are not mounted but that contain a filesystem"? Commented Apr 17, 2020 at 12:46
  • Exactly, because /dev/sda partitions are obviously mounted, at least in my setup.
    – Krackout
    Commented Apr 17, 2020 at 12:47

1 Answer 1

3

If you simplify your requirement to being a list of partitions that are not mounted but that contain a filesystem, you can do this using awk

lsblk -rno name,size,mountpoint,fstype | awk 'NF==3 {print $1, $3}'

The awk filter considers lines from lsblk that have exactly three space-separated fields, and prints the first and third field.

Sample output

lsblk -rno name,size,mountpoint,fstype
mmcblk0 29.8G
mmcblk0p1 63M  vfat
mmcblk0p2 4G / ext4

lsblk -rno name,size,mountpoint,fstype | awk 'NF==3 {print $1, $3}'
mmcblk0p1 vfat

If you want size too, just omit the output selector

lsblk -rno name,size,mountpoint,fstype | awk 'NF==3'
mmcblk0p1 63M  vfat
3
  • Not bad, I'd like to have the info on size though. If I don't get any better response, I'll mark it as answer. Thank you.
    – Krackout
    Commented Apr 17, 2020 at 13:37
  • Well, it was quite simple, to add size also: lsblk -rno name,size,mountpoint,fstype | awk 'NF==3 {print $1, $2, $3}'
    – Krackout
    Commented Apr 17, 2020 at 14:21
  • @Krackout there you go; answer updated to include size Commented Apr 17, 2020 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .