如何在 Linux 中清理 Snap 包的版本
Snap
![](/data/attachment/album/202202/24/153025zxi8112125hrxrl8.jpg)


Snap 软件包并不是每个人都喜欢的，但它们是 Ubuntu 生态系统中不可或缺的一部分。


它有其优点和缺点。其中一个缺点是，Snap 包通常体积较大，占用大量的磁盘空间。如果你的磁盘空间不够用，特别是在根分区上，这可能是一个问题。


让我分享一个巧妙的技巧，你可以用它来减少 Snap 包使用的磁盘空间。


### 清理旧的 Snap 包版本以释放磁盘空间


与 snap 有关的系统文件都存放在 `/var/lib/snapd` 目录下。根据你所安装的 Snap 包的数量，这个目录的大小可能在几 GB。不要只听我的一面之词。通过 [使用 du 命令检查目录大小](https://linuxhandbook.com/find-directory-size-du-command/) 来进行评估。



```
$ sudo du -sh /var/lib/snapd
5.4G    /var/lib/snapd

```

你也可以使用磁盘使用分析器这个 GUI 工具来查看 [Ubuntu 的磁盘使用情况](https://itsfoss.com/check-free-disk-space-linux/)。


![Snap disk usage](/data/attachment/album/202202/24/153035vxlv8ajvl1aailvf.png)


这可真够多的，对吧？你可以在这里腾出一些磁盘空间。根据设计，Snap 至少会在你的系统上保留一个你所安装的软件包的旧版本。你可以通过使用 Snap 命令看到这种行为：



```
snap list --all

```

你应该看到同一个软件包被列了两次，而且版本和修订号都不同。


![Snap keeps at least two versions of each package](/data/attachment/album/202202/24/153037c2mj5h5jl12o2865.png)


为了释放磁盘空间，你可以删除额外的软件包版本。你怎么知道要删除哪一个呢？你可以看到，这些较旧的软件包被标记为“禁用”。


不要担心。你不需要手动操作。由于 Alan Pope 在 [Snapcraft](https://snapcraft.io/) 团队工作时写的一个灵巧的 bash 脚本，有一种自动的方法可以做到。


我希望你知道 [如何创建和运行 bash shell 脚本](https://itsfoss.com/run-shell-script-linux/)。基本上，创建一个名为 `clean-snap.sh` 的新文件，并在其中添加以下几行。



```
#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

```

保存它并关闭编辑器。要运行这个脚本，把它放在你的主目录中，然后 [在 Ubuntu 中打开终端](https://itsfoss.com/open-terminal-ubuntu/)，运行这个命令：



```
sudo bash clean-snap.sh

```

你可以看到，它开始删除旧版本的软件包。


![Removing old snap package versions](/data/attachment/album/202202/24/153039v1i90stbaa0aa101.png)


如果你现在检查 Snap 使用的磁盘空间，你会发现现在的目录大小已经减少了。



```
$ sudo du -sh /var/lib/snapd
3.9G    /var/lib/snapd

```

如果这对你有用，你可以偶尔运行这个命令。


#### 这个脚本是如何工作的？


如果你对这个脚本的作用感到好奇，让我来解释一下。


你已经看到了 `snap list -all` 命令的输出。它的输出被传递给 [awk 命令](https://linuxhandbook.com/awk-command-tutorial/)。Awk 是一个强大的脚本工具。


`awk '/disabled/{print $1, $3}'` 部分在每一行中寻找字符串 `disabled`，如果找到它，它将提取第一列和第三列。


这个输出被进一步传递给 `while` 和 `read` 命令的组合。读取命令获取第一列的 Snap 包名和第三列的修订号变量。


然后，这些变量被用来运行 `snap remove` 命令，用 Snap 包名和它的修订号来删除。


只要发现有包含 `disabled` 字符串的行，就会运行 `while` 循环。


如果你对 shell 脚本略知一二，这一切就很容易理解了。如果你不熟悉，我们有一个 [初学者的 bash 教程系列](https://linuxhandbook.com/tag/bash-beginner/) 给你。


### 你拿回了你的空间了吗？


你可能会看到一些论坛建议将 Snap 软件包的保留值设置为 2。



```
sudo snap set system refresh.retain=2

```

我认为现在不需要了。现在 Snap 的默认行为是为任何软件包保存两个版本。


总而言之，如果你的空间不够用，摆脱额外的软件包版本肯定是 [释放 Ubuntu 磁盘空间的方法](https://itsfoss.com/free-up-space-ubuntu-linux/) 之一。


如果这个教程帮助你释放了一些空间，请在评论区告诉我。




---


via: <https://itsfoss.com/clean-snap-packages/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Snap packages are not everyone’s favorite but they are an integral part of the Ubuntu ecosystem.

It has its pros and cons. One of the negatives is that Snap packages are usually bigger in size and take a lot of disk space.

This could be a problem if you are running out of disk space, specially on the root partition.

Let me share a neat trick that you could use to cut down the disk spaced used by Snap packages.

## Cleaning up old Snap package versions to free disk space

The system files related to snap are stored in the /var/lib/snapd directory. Based on the number of Snap packages you have installed, this directory size could be in several GBs.

Don’t just take my word for it. Do an assesement by [using the du command to check the directory size.](https://linuxhandbook.com/find-directory-size-du-command/?ref=itsfoss.com)

```
abhishek@its-foss:~$ sudo du -sh /var/lib/snapd
5.4G /var/lib/snapd
```


You may also use the Disk Usage Analyzer GUI tool to see the [disk usage in Ubuntu](https://itsfoss.com/check-free-disk-space-linux/).

![snap disk usage](https://itsfoss.com/content/images/wordpress/2022/02/snap-disk-usage-800x323.png)

That’s a lot, right? You could free up some disk space here.

By design, Snap keeps at least one older version of the packages you have installed on your system.

You can see this behavior by using the Snap command:

`snap list --all`


You should see the same package listed twice with different version and revision number.

![snap keeps two versions of each package](https://itsfoss.com/content/images/wordpress/2022/02/snap-keeps-two-versions-of-each-package-800x347.png)

To free up disk space, you can delete the additional package versions. How do you know which one to delete? You can see that these older packages are labeled ‘disabled’.

Don’t worry. You don’t have to manually do it. There is sort of automatic way to do it thanks to a nifty bash script written by Alan Pope while he was working in the [Snapcraft](https://snapcraft.io/?ref=itsfoss.com) team.

I hope you know [how to create and run a bash shell script](https://itsfoss.com/run-shell-script-linux/). Basically, create a new file named clean-snap.sh and add the following lines to it.

```
#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
```


Save it and close the editor.

To run this script, keep it in your home directory and then [open the terminal in Ubuntu](https://itsfoss.com/open-terminal-ubuntu/) and run this command:

`sudo bash clean-snap.sh`


You can see that it starts removing the older version of packages.

![removing old snap package versions](https://itsfoss.com/content/images/wordpress/2022/02/removing-old-snap-package-versions-800x445.png)

If you check the disk space used by Snap now, you’ll see that the directory size is reduced now.

```
abhishek@its-foss:~$ sudo du -sh /var/lib/snapd
3.9G /var/lib/snapd
```


If this works for you, you could run this command occasionally.

### How does this script work?

If you are curious about what does this script do, let me explain.

You have already seen the output of the “snap list –all” command. It’s output is passed to the [awk command](https://linuxhandbook.com/awk-command-tutorial/?ref=itsfoss.com). Awk is a powerful scripting tool.

The awk ‘/disabled/{print $1, $3}’ part looks for the string ‘disabled’ in each row and if it is found, it extracts the first column and third column.

This output is further passed to a combination of while and read command. Read command gets the value of first column to snapname and third column to revision variable.

These variables are then used to run the snap remove command to delete with the name of the span package name and its revision number.

The while loop runs as long as there are rows found with ‘disabled’ string in it.

This all makes sense easily if you know a little bit about shell scripting. If you are not familiar with, we have a [bash tutorial series for beginners](https://linuxhandbook.com/tag/bash-beginner/?ref=itsfoss.com) for you.

## Did you get your GBs back?

You may see some forums advising to set up the Snap package retention value to 2.

`sudo snap set system refresh.retain=2`


I don’t think it’s needed anymore. Snap’s default behavior now is to store total 2 versions for any package.

Altogether, if you are running out of space, getting rid of the additional package version could surely one of the [ways to free up disk space on Ubuntu](https://itsfoss.com/free-up-space-ubuntu-linux/).

If this tutorial helped you free some space, let me know in the comment section.