Can I shorten this filter, that finds disk sizes over 100G?

You can specify the form of output you want from lsblk:

% lsblk -nblo NAME,SIZE
mmcblk0   15931539456
mmcblk0p1   268435456
mmcblk0p2 15662038528

Options used:

-b, --bytes
      Print the SIZE column in bytes rather than in human-readable format.

-l, --list
      Use the list output format.

-n, --noheadings
      Do not print a header line.

-o, --output list
      Specify which output columns to print.  Use --help to get a list of  all  supported
      columns.

Then the filtering is easier:

% lsblk -nblo NAME,SIZE | awk '$2 > 4*2^30 {print $1}' # greater than 4 GiB
mmcblk0
mmcblk0p2

In your case, that'd be 100*2^30 for 100GiB or 100e9/1e11 for 100GB.


You can also tell lsblk to output in JSON format and do the filtering with jq:

$ lsblk -Jb | jq -r '..|select(.size? >= 1e11).name'
sda
sda2

Or:

$ lsblk -Jb | jq -r '..|select(.type? == "disk" and .size? >= 1e11).name'
sda

To limit to entries of type disk.

(1e11 being 100 GB. Replace with 107374182400 (or 100*1024*1024*1024) for 100 GiB. Because of rounding, lsblk itself without -b reports 100G for sizes ranging from about 99.9278 to 100.0488 GiB (for some reason))

With lsblk -OJb, lsblk reports all available information which lets you do a more fined-grained selection or output more or more relevant information.

You could also get the information directly from /sys. With zsh:

$ printf '%s\n' /sys/class/block/*(e'[(($(<$REPLY/size) * 512 >= 1e11))]':t)
sda
sda2

try

lsblk| awk '$4 ~ /G$/ && $4+0 > 100 {print $1}'

this will grep and filter at same time.

  • $4 ~ /G$/ get filed with size in G
  • $4+0 > 100 get size over 100G
  • {print $1} print NAME

as a rule you should never need to use grep and awk in same pipe.

to get only disk (and no partition) : awk filtering

lsblk| awk '$4 ~ /G$/ && $4+0 > 100 && $6 == "disk"  {print $1}'

where

  • $6 == "disk" select only column with disk

to get only disk (and no partition) : lsblk filtering

lsblk --nodeps| awk '$4 ~ /G$/ && $4+0 > 100 {print $1}'

where

  • --nodeps : -d, --nodeps don't print slaves or holders

Tags:

Grep

Awk

Disk

Lsblk