AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1354124
Accepted
Grzegorz Michalak
Grzegorz Michalak
Asked: 2021-07-27 01:26:52 +0800 CST2021-07-27 01:26:52 +0800 CST 2021-07-27 01:26:52 +0800 CST

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

  • 772

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

128

如果安装了两个硬盘驱动器,它会给出输出

128/512

它应该适用于 sata 和 nvme 驱动器

command-line
  • 3 3 个回答
  • 203 Views

3 个回答

  • Voted
  1. marosg
    2021-07-27T01:40:43+08:002021-07-27T01:40:43+08:00

    目前尚不清楚您的问题是什么。我希望您不希望有人编写整个脚本,而是询问如何获得要使用的数字。

    使用 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)
    ...
    
    • 1
  2. Best Answer
    Cas
    2021-07-30T08:53:34+08:002021-07-30T08:53:34+08:00

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

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

    它给出这样的输出:

    1TB/750GB
    

    否则你不会知道它是否是 TB/GB/MB,所以这就是我保留这些的原因。

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

    解释:

    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/: 现在我们将结果放在一个列表中,我们可以使用粘贴命令将它们放在一起,使用“/”作为分隔符。

    • 1
  3. sudodus
    2021-08-02T07:40:47+08:002021-08-02T07:40:47+08:00

    下面的一行命令应该做你想做的事。

    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 (gibibytes) 所以我经常使用普通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
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve