如何显示已安装硬盘的大小

如何显示已安装硬盘的大小

我想编写一个简单的 Linux 脚本,显示已安装硬盘的实际大小。如果有 128GB SSD,lsblk 显示小于 128。我需要检查内存的实际大小并像这样打印:已安装 128GB = 脚本给出输出

128

如果安装了两个硬盘,则会输出

128/512

它应该可以在 sata 和 nvme 驱动器上工作

答案1

它需要 sudo 才能使用,但是它可以工作:

sudo lshw | grep -Pzo "\*-(disk|namespace)(\n.*)+?\s+size:.*?\(\K\d+\w+" | tr "\0" "\n" | paste -sd/

它给出如下输出:

1TB/750GB

否则您将不知道它是 TB/GB/MB,这就是我保留它们的原因。

编辑:我注意到.*正则表达式中的 是不需要的。上面的命令已更新。

解释:

sudo lshw:好吧...列出硬件。我们需要 sudo 才能查看系统中的驱动器。这将是我们的信息来源。

grep -Pzo:我们将使用正则表达式来获取所需的信息

-P = activate perl regex
-z = treat everything as one long line; this is required because we're going to use regex over multiple lines
-o = instead of showing the output and marking the result red, just show the result

regex(提示:删除 -o(又名 -Pz)并逐个添加下面的正则表达式,以查看逐步发生的情况;红色文本是正则表达式匹配的内容,因此您可以逐步看到我们如何接近期望的结果以及每一步对输出的改变):

\*-(disk|namespace) = Find all text that is "*-disk" or "*-namespace". We need to escape (= \) the "*" because in regex it means zero or more, but we don't want that, we want to search for a literal "*".
(\n.*)+ = Keep adding (the "+" = one or more) lines ("\n" = go to the next line; ".*" = everything on that line) to the matched text; you'll see that now everything under the first match of "\*-(disk|namespace)" is red.
?\s+size: = we keep adding lines until we come across the first ("?" = non-greedy a.k.a. the first match instead of the last match) match of one or more whitespaces (= "\s+"; "\s" is whitespace (tab, space, etc.); "+" is one or more) and then "size:"; you'll see that were coming closer to the desired number in the output.
.* = match the rest of that line,
?\( = until the first match (= ?) of a "(", which we need to escape because it is used in regex (you can see it being used in the first part of the regex here).
\K\d+\w+ = match a number (= \d) one or more times (= "+") and after that a word character (= \w) one or more times (= "+"). We now have the desired text in the match, but we don't want all the matched text before that in the output, so we put a "\K" before the desired text to remove the matched text before the "\K" from the output. It is still required to match, it just isn't included in the output. This makes all the regex before it a positive lookbehind (look it up; "positive lookbehind perl regex") with regex capabilities.

tr "\0" "\n":您将看到我们匹配到了所需的文本,不多也不少。当我们再次添加 -o 时,您将看到结果显示的方式很奇怪(一个接一个)。这是因为它们被您看不到的空字符 (= \0) 分隔,而不是换行符。这是 grep 的“-z”选项造成的。为了在正常列表中显示它们,我们将使用 用换行符替换空字符tr

paste -sd/:现在我们在列表中有了结果,我们可以使用粘贴命令将它们放在一起,使用“/”作为分隔符。

答案2

不清楚你的问题是什么。我希望你不是想让别人写整个脚本,而是问如何获得你想要的数字。

使用 lshw,这是我的系统的(已编辑)输出,存在一个 NVMe 和一个 SSD

$ lshw -c disk
  *-namespace               
       description: NVMe namespace
       logical name: /dev/nvme0n1
       size: 953GiB (1024GB)
...
  *-disk
       description: ATA Disk
       product: Samsung SSD 850
       logical name: /dev/sda
       size: 931GiB (1TB)
...

答案3

下面的单行命令应该可以完成您想要的操作。

for i in $(lsblk -bdno name,size|tr -s ' ' '_'); do j=${i##*_};j=$(((j+500000000)/1000000000));printf "${i%_*}\t$j\tGB\n";done

您可以将一个函数放在~/.bashrc文件中别名附近,以便使用简短命令运行它,例如mydrives

function mydrives { for i in $(lsblk -bdno name,size|tr -s ' ' '_'); do j=${i##*_};j=$(((j+500000000)/1000000000));printf "${i%_*}\t$j\tGB\n";done }

对我来说,它是这样工作的,

source ~/.bashrc

以及在您修改文件后打开的所有终端窗口中~/.bashrc

在我的电脑上打印如下

$ mydrives
sda     256     GB
sdb     4001    GB
sr0     1       GB
nvme0n1 250     GB

我习惯使用 GiB(吉字节),因此我经常使用普通lsblk命令

lsblk

或者当我想了解更多详细信息并且有一个宽阔的终端窗口时

lsblk -fm

或者其他

lsblk -m

与我建议的命令相匹配的命令,但使用 mibibytes、gibibytes、tibibytes(以 2 为基数,而不是以 10 为基数)将是

$ lsblk -dno name,size
sda     238,5G
sdb       3,7T
sr0      1024M
nvme0n1 232,9G

相关内容