如何列出 Ubuntu 20.04 中安装的每个 snap 包的大小?

如何列出 Ubuntu 20.04 中安装的每个 snap 包的大小?

用户如何列出 Ubuntu 20.04 中已安装的每个 snap 包的大小?目的是概览每个已安装的 snap 包所用的磁盘空间。该命令snap list无法执行此操作。此外,应用程序Disk Usage Analyzer无法显示更多内容/var/lib/snapd/snaps

磁盘使用情况分析器

答案1

所有snap软件包都存储在 中/var/lib/snapd/snaps/。此目录包含软件包的所有修订版本和名称snap。您可以ls在目录中运行以列出所有 snap 软件包。使用-lh参数 withls将为您提供更详细的输出以及软件包(及其每个修订版本)的大小。因此,您可以运行以下命令列出所有软件包snap及其安装大小:

ls -lh /var/lib/snapd/snaps/

或者,您可以使用du带有参数的命令-hcs列出所有已安装包的大小:

du -hcs /var/lib/snapd/snaps/* 

答案2

snap list局限性

在撰写本文时(2022 年 7 月),snap list命令确实不支持对其输出的列进行配置,这是一种硬编码行为: https://github.com/snapcore/snapd/blob/293c48dc2b97f141d1bc35d5ea91c2e3484f01fd/cmd/snap/cmd_list.go

使用 snap API

然而,根据官方文件,https://snapcraft.io/docs/using-the-api其中,snap daemon( snapd) 通过 unix socket 公开其 API /run/snapd.socket

$ netstat --unix --listening --programs | grep snap
unix  2      [ ACC ]     STREAM     LISTENING     25415    4229/systemd        /run/user/1000/snapd-session-agent.socket
unix  2      [ ACC ]     STREAM     LISTENING     19241    -                   /run/snapd.socket
unix  2      [ ACC ]     STREAM     LISTENING     19242    -                   /run/snapd-snap.socket

端点/snapshttps://snapcraft.io/docs/snapd-api#heading--snaps)返回有关所有已安装快照的机器可读(JSON)信息。

其中还包括"installed-size"财产。

正如前面提到的帮助页面所建议的那样(https://snapcraft.io/docs/using-the-api#heading--accessing),您可以通过发出以下命令来获取此信息

curl --silent --show-error --unix-socket /run/snapd.socket http://localhost/v2/snaps?select=all

然后,您可以使用任何您喜欢的工具来处理和分析这些数据,jq或者python其他什么。

列出每个已安装 snap 包大小的示例

基于我的系统 3.0.1 的数据示例ruby

命令(shell):

ruby -rjson -rtime -rterminal-table -e '
json = JSON.parse(ARGF.read)

columns = [*%w(name revision), %w(publisher display-name), *%w(confinement install-date installed-size mounted-from)]

formatters = {
    "install-date" => lambda { Time.parse(_1).getlocal.strftime("%x %X") rescue "" },
    "installed-size" => lambda { `numfmt --to=iec #{_1}`.chop },
    "mounted-from" => lambda { File.dirname(_1) }
}

result = json["result"].map do |app|
    columns.map do |column|
        app.dig *column
    end
end

puts Terminal::Table.new(
    headings: [
        "#",
        *columns.map{ _1.respond_to?(:join) ? _1.join("-") : _1 }
    ],
    rows: result.sort_by{ _1[columns.index("name")] }.map.with_index do |row, index|
        [
            index + 1,
            *row.map.with_index do |cell, index|
                formatters[columns[index]] ? formatters[columns[index]][cell] : cell
            end
        ]
    end <<
    Array.new(columns.size).tap do |emptyrow|
            index = columns.index("installed-size")

            emptyrow[index + 1] = formatters["installed-size"][
                result.reduce(0) do |total, row|
                    total + row[index]
                end
            ]
    end
)' <(curl --silent --show-error --unix-socket /run/snapd.socket http://localhost/v2/snaps?select=all)

输出:

+----+-----------------------------------+----------+------------------------+-------------+-------------------+----------------+----------------------+
| #  | name                              | revision | publisher-display-name | confinement | install-date      | installed-size | mounted-from         |
+----+-----------------------------------+----------+------------------------+-------------+-------------------+----------------+----------------------+
| 1  | atom                              | 282      | Snapcrafters           | classic     |                   | 286M           | /var/lib/snapd/snaps |
| 2  | atom                              | 286      | Snapcrafters           | classic     | 07/14/22 21:41:12 | 285M           | /var/lib/snapd/snaps |
| 3  | bare                              | 5        | Canonical              | strict      | 09/27/21 03:08:28 | 4.0K           | /var/lib/snapd/snaps |
| 4  | chromium                          | 2033     | Canonical              | strict      |                   | 134M           | /var/lib/snapd/snaps |
| 5  | chromium                          | 2036     | Canonical              | strict      | 07/25/22 23:17:33 | 134M           | /var/lib/snapd/snaps |
| 6  | code                              | 101      | Visual Studio Code     | classic     |                   | 222M           | /var/lib/snapd/snaps |
| 7  | code                              | 102      | Visual Studio Code     | classic     | 07/19/22 00:06:55 | 222M           | /var/lib/snapd/snaps |
| 8  | core                              | 13250    | Canonical              | strict      |                   | 112M           | /var/lib/snapd/snaps |
| 9  | core                              | 13425    | Canonical              | strict      | 07/14/22 21:38:16 | 114M           | /var/lib/snapd/snaps |
| 10 | core18                            | 2409     | Canonical              | strict      |                   | 56M            | /var/lib/snapd/snaps |
| 11 | core18                            | 2538     | Canonical              | strict      | 07/19/22 00:06:55 | 56M            | /var/lib/snapd/snaps |
| 12 | core20                            | 1581     | Canonical              | strict      |                   | 62M            | /var/lib/snapd/snaps |
| 13 | core20                            | 1587     | Canonical              | strict      | 07/27/22 00:03:07 | 62M            | /var/lib/snapd/snaps |
| 14 | digikam                           | 52       | KDE                    | strict      |                   | 184M           | /var/lib/snapd/snaps |
| 15 | digikam                           | 53       | KDE                    | strict      | 03/03/22 23:04:01 | 184M           | /var/lib/snapd/snaps |
| 16 | gnome-3-28-1804                   | 145      | Canonical              | strict      |                   | 163M           | /var/lib/snapd/snaps |
| 17 | gnome-3-28-1804                   | 161      | Canonical              | strict      | 07/27/21 00:54:00 | 165M           | /var/lib/snapd/snaps |
| 18 | gnome-3-34-1804                   | 72       | Canonical              | strict      |                   | 219M           | /var/lib/snapd/snaps |
| 19 | gnome-3-34-1804                   | 77       | Canonical              | strict      | 11/24/21 14:53:44 | 219M           | /var/lib/snapd/snaps |
| 20 | gnome-3-38-2004                   | 99       | Canonical              | strict      |                   | 249M           | /var/lib/snapd/snaps |
| 21 | gnome-3-38-2004                   | 112      | Canonical              | strict      | 07/14/22 21:42:19 | 401M           | /var/lib/snapd/snaps |
| 22 | gtk-common-themes                 | 1534     | Canonical              | strict      |                   | 82M            | /var/lib/snapd/snaps |
| 23 | gtk-common-themes                 | 1535     | Canonical              | strict      | 07/14/22 21:39:41 | 92M            | /var/lib/snapd/snaps |
| 24 | kde-frameworks-5-core18           | 32       | KDE                    | strict      | 03/15/20 10:20:17 | 261M           | /var/lib/snapd/snaps |
| 25 | kde-frameworks-5-qt-5-15-3-core20 | 5        | KDE                    | strict      |                   | 319M           | /var/lib/snapd/snaps |
| 26 | kde-frameworks-5-qt-5-15-3-core20 | 8        | KDE                    | strict      | 11/16/21 19:48:35 | 425M           | /var/lib/snapd/snaps |
| 27 | kde-frameworks-5-qt-5-15-core20   | 14       | KDE                    | strict      | 04/21/21 22:07:58 | 324M           | /var/lib/snapd/snaps |
| 28 | musescore                         | 216      | MuseScore              | strict      |                   | 176M           | /var/lib/snapd/snaps |
| 29 | musescore                         | 228      | MuseScore              | strict      | 05/22/22 18:36:22 | 176M           | /var/lib/snapd/snaps |
| 30 | nongnu-gsequencer                 | 243      | Joël Krähemann         | strict      |                   | 256M           | /var/lib/snapd/snaps |
| 31 | nongnu-gsequencer                 | 252      | Joël Krähemann         | strict      | 07/14/22 21:41:52 | 256M           | /var/lib/snapd/snaps |
| 32 | scrcpy                            | 386      | sisco311               | strict      | 07/31/22 00:49:17 | 83M            | /var/lib/snapd/snaps |
| 33 | shotcut                           | 692      | Meltytech, LLC         | classic     |                   | 82M            | /var/lib/snapd/snaps |
| 34 | shotcut                           | 765      | Meltytech, LLC         | classic     | 07/14/22 21:40:48 | 145M           | /var/lib/snapd/snaps |
| 35 | snap-store                        | 547      | Canonical              | strict      |                   | 51M            | /var/lib/snapd/snaps |
| 36 | snap-store                        | 558      | Canonical              | strict      | 11/24/21 14:53:52 | 55M            | /var/lib/snapd/snaps |
| 37 | whalebird                         | 119      | AkiraFukushima         | strict      |                   | 98M            | /var/lib/snapd/snaps |
| 38 | whalebird                         | 121      | AkiraFukushima         | strict      | 07/14/22 21:41:11 | 99M            | /var/lib/snapd/snaps |
|    |                                   |          |                        |             |                   | 6.4G           |
+----+-----------------------------------+----------+------------------------+-------------+-------------------+---------------------------------------+

总体产量与 相匹配snap list --all

我们可以看到所有这些快照都位于一个(默认)目录中。

并且运行

$ du -h /var/lib/snapd/snaps/{atom*,bare*,chromium*}
286M    /var/lib/snapd/snaps/atom_282.snap
285M    /var/lib/snapd/snaps/atom_286.snap
4,0K    /var/lib/snapd/snaps/bare_5.snap
134M    /var/lib/snapd/snaps/chromium_2033.snap
134M    /var/lib/snapd/snaps/chromium_2036.snap
$ du -hs /var/lib/snapd/snaps
6,5G    /var/lib/snapd/snaps

将返回紧密匹配的值。

结论

我们可以看到,已经接受的答案中建议的测量目录条目占用空间的方法/var/lib/snapd/snaps并没有错,这一点可以从 API 数据和官方解释中看出。https://snapcraft.io/docs/system-snap-directory

相关内容