自安装以来,我向 15.04 系统添加了不少 PPA,因为我想要一个特定的应用程序。但有时我稍后会再次删除该包,因为它不起作用,不是我真正想要的,或者只是因为我不再需要它。我不知道这是否有区别,但有些存储库是随系统一起安装的,add-apt-repository
而其他存储库只是通过手动创建文件的命令安装的/etc/apt/sources.list.d/
。
现在我该如何检查我是否安装了来自 PPA 的任何软件包?如果没有,如何从我的软件源中删除它?此过程应自动适用于所有 PPA。
答案1
这是一个脚本。如果没有参数,脚本会列出一些信息。如果使用--delete
,则list
文件将被删除,如果不软件包已安装。
#!/usr/bin/env bash
for f in /etc/apt/sources.list.d/*.list; do
grep -Po "(?<=^deb\s).*?(?=#|$)" "$f" | while read -r ENTRY ; do
echo "ENTRY: $ENTRY"
# Sanitize entry line by removing stuff between square brackets
ENTRY=$(echo "$ENTRY" | sed 's/\[.*\] //')
HOST=$(cut -d/ -f3 <<< "$ENTRY")
if [ "ppa.launchpad.net" = "$HOST" ]; then
USER=$(cut -d/ -f4 <<< "$ENTRY")
PPA=$(cut -d/ -f5 <<< "$ENTRY")
packageCount=$(sed -e '/^Package:/!d' -e 's/Package: \(.*\)$/\1/g' /var/lib/apt/lists/*"$USER"*"$PPA"*Packages | xargs dpkg -l 2>/dev/null | grep -c '^ii')
echo "PPA: ppa:$USER/$PPA"
echo "FILENAME: $f"
echo "$packageCount package(s) installed"
if [ "$packageCount" -eq 0 ] && [ "$1" == "--delete" ]; then
sudo rm "$f" && echo "$f deleted"
fi
echo
else
USER=$(cut -d/ -f3 <<< "$ENTRY")
PPA=$(cut -d/ -f4 <<< "$ENTRY")
packageCount=$(sed -e '/^Package:/!d' -e 's/Package: \(.*\)$/\1/g' /var/lib/apt/lists/*"$USER"*Packages | xargs dpkg -l 2>/dev/null | grep -c '^ii')
echo "REPOSITORY: $USER/$PPA"
echo "FILENAME: $f"
echo "$packageCount package(s) installed"
if [ "$packageCount" -eq 0 ] && [ "$1" == "--delete" ]; then
sudo rm "$f" && echo "$f deleted"
fi
echo
fi
done
done
将上述代码复制到新文件中,例如:
mkdir -p ~/bin
nano ~/bin/checkPPAs
使脚本可执行
chmod +x ~/bin/checkPPAs
然后运行它
~/bin/checkPPAs
列出所有存储库和已安装的软件包的数量。
使用以下命令启动脚本
~/bin/checkPPAs --delete
删除list
文件。
我的系统上的示例输出
ENTRY: http://ppa.launchpad.net/webupd8team/y-ppa-manager/ubuntu vivid main
PPA: ppa:webupd8team/y-ppa-manager
FILENAME: /etc/apt/sources.list.d/webupd8team-ubuntu-y-ppa-manager-vivid.list
3 package(s) installed
ENTRY: http://ppa.launchpad.net/nilarimogard/webupd8/ubuntu vivid main
PPA: ppa:nilarimogard/webupd8
FILENAME: /etc/apt/sources.list.d/nilarimogard-ubuntu-webupd8-vivid.list
5 package(s) installed
ENTRY: http://ppa.launchpad.net/obsproject/obs-studio/ubuntu vivid main
PPA: ppa:obsproject/obs-studio
FILENAME: /etc/apt/sources.list.d/obsproject-ubuntu-obs-studio-vivid.list
1 package(s) installed
ENTRY: http://archive.getdeb.net/ubuntu vivid-getdeb apps
REPOSITORY: archive.getdeb.net/ubuntu vivid-getdeb apps
FILENAME: /etc/apt/sources.list.d/getdeb.list
7 package(s) installed
ENTRY: http://ppa.launchpad.net/psi-plus/ppa/ubuntu vivid main
PPA: ppa:psi-plus/ppa
FILENAME: /etc/apt/sources.list.d/psi-plus-ubuntu-ppa-vivid.list
15 package(s) installed
ENTRY: http://ppa.launchpad.net/libreoffice/ppa/ubuntu vivid main
PPA: ppa:libreoffice/ppa
FILENAME: /etc/apt/sources.list.d/libreoffice-ubuntu-ppa-vivid.list
24 package(s) installed
ENTRY: https://deb.nodesource.com/node_0.12 vivid main
REPOSITORY: deb.nodesource.com/node_0.12 vivid main
FILENAME: /etc/apt/sources.list.d/nodesource.list
1 package(s) installed
ENTRY: http://dl.google.com/linux/chrome/deb/ stable main
REPOSITORY: dl.google.com/linux
FILENAME: /etc/apt/sources.list.d/google-chrome.list
2 package(s) installed
ENTRY: http://ppa.launchpad.net/gnome3-team/gnome3/ubuntu vivid main
PPA: ppa:gnome3-team/gnome3
FILENAME: /etc/apt/sources.list.d/gnome3-team-ubuntu-gnome3-vivid.list
273 package(s) installed
ENTRY: http://ppa.launchpad.net/noobslab/apps/ubuntu vivid main
PPA: ppa:noobslab/apps
FILENAME: /etc/apt/sources.list.d/noobslab-ubuntu-apps-vivid.list
0 package(s) installed
ENTRY: http://ppa.launchpad.net/ubuntuhandbook1/corebird/ubuntu vivid main
PPA: ppa:ubuntuhandbook1/corebird
FILENAME: /etc/apt/sources.list.d/ubuntuhandbook1-ubuntu-corebird-vivid.list
1 package(s) installed
ENTRY: http://ppa.launchpad.net/webupd8team/unstable/ubuntu vivid main
PPA: ppa:webupd8team/unstable
FILENAME: /etc/apt/sources.list.d/webupd8team-ubuntu-unstable-vivid.list
1 package(s) installed
ENTRY: http://download.mono-project.com/repo/debian wheezy main
REPOSITORY: download.mono-project.com/repo
FILENAME: /etc/apt/sources.list.d/mono-xamarin.list
166 package(s) installed
ENTRY: http://ppa.launchpad.net/otto-kesselgulasch/gimp-edge/ubuntu vivid main
PPA: ppa:otto-kesselgulasch/gimp-edge
FILENAME: /etc/apt/sources.list.d/otto-kesselgulasch-ubuntu-gimp-edge-vivid.list
5 package(s) installed
ENTRY: http://ppa.launchpad.net/numix/ppa/ubuntu vivid main
PPA: ppa:numix/ppa
FILENAME: /etc/apt/sources.list.d/numix-ubuntu-ppa-vivid.list
3 package(s) installed
ENTRY: http://ppa.launchpad.net/webupd8team/atom/ubuntu vivid main
PPA: ppa:webupd8team/atom
FILENAME: /etc/apt/sources.list.d/webupd8team-ubuntu-atom-vivid.list
1 package(s) installed
ENTRY: https://packages.graylog2.org/repo/debian/ trusty 1.0
REPOSITORY: packages.graylog2.org/repo
FILENAME: /etc/apt/sources.list.d/graylog.list
3 package(s) installed
ENTRY: http://ppa.launchpad.net/ubuntu-mate-dev/ppa/ubuntu vivid main
PPA: ppa:ubuntu-mate-dev/ppa
FILENAME: /etc/apt/sources.list.d/ubuntu-mate-dev-ubuntu-ppa-vivid.list
0 package(s) installed
ENTRY: http://ppa.launchpad.net/gnome3-team/gnome3-staging/ubuntu vivid main
PPA: ppa:gnome3-team/gnome3-staging
FILENAME: /etc/apt/sources.list.d/gnome3-team-ubuntu-gnome3-staging-vivid.list
268 package(s) installed
ENTRY: http://debian.koha-community.org/koha stable main
REPOSITORY: debian.koha-community.org/koha stable main
FILENAME: /etc/apt/sources.list.d/koha.list
0 package(s) installed
ENTRY: http://ppa.launchpad.net/pgavin/ghdl/ubuntu trusty main
PPA: ppa:pgavin/ghdl
FILENAME: /etc/apt/sources.list.d/pgavin-ubuntu-ghdl-vivid.list
0 package(s) installed
ENTRY: http://ppa.launchpad.net/ubuntu-mate-dev/vivid-mate/ubuntu vivid main
PPA: ppa:ubuntu-mate-dev/vivid-mate
FILENAME: /etc/apt/sources.list.d/ubuntu-mate-dev-ubuntu-vivid-mate-vivid.list
3 package(s) installed
ENTRY: http://ppa.launchpad.net/webupd8team/sublime-text-3/ubuntu vivid main
PPA: ppa:webupd8team/sublime-text-3
FILENAME: /etc/apt/sources.list.d/webupd8team-ubuntu-sublime-text-3-vivid.list
1 package(s) installed
ENTRY: http://ppa.launchpad.net/webupd8team/java/ubuntu vivid main
PPA: ppa:webupd8team/java
FILENAME: /etc/apt/sources.list.d/webupd8team-ubuntu-java-vivid.list
1 package(s) installed
ENTRY: http://dl.google.com/linux/chrome/deb/ stable main
REPOSITORY: dl.google.com/linux
FILENAME: /etc/apt/sources.list.d/google.list
2 package(s) installed
ENTRY: http://dl.google.com/linux/chrome/deb/ stable main
REPOSITORY: dl.google.com/linux
FILENAME: /etc/apt/sources.list.d/google-chrome-beta.list
2 package(s) installed
答案2
我不知道有没有自动化的方法,但是这种方法相当快:
启动 synaptic 包管理器并在出现提示时输入密码
在左下角,点击起源
现在,系统上的所有 ppa 都将列在左栏顶部。您可以单击列表,查看每个 ppa 中是否安装了任何软件。(如果有,右栏中软件包名称旁边会有一个绿色框)。如果 ppa 中有许多软件包(太多而无法一次在屏幕上看到),您可以单击年代按状态对它们进行排序。
笔记:如果您有任何未使用的 ppa:
- 点击设置在顶部菜单中
- 然后存储库
- 软件和更新 将显示窗口。
- 从此窗口中,你可以从中删除未使用的 ppa其他软件标签。
答案3
在看到接受的答案有一个关键错误后,我不得不修复它并将其发布在这里。
AB 的答案遍历每个list
文件中的条目,如果该文件中的任何条目在系统上安装了 0 个包,则无论有多少条目有包,它都会删除整个文件。
它还会完全忽略任何不符合预定义格式的条目。它不会考虑这些条目。
该脚本还会处理匹配的条目。它不会触碰空文件。
因此我修改了脚本如下:
#!/usr/bin/env bash
for f in /etc/apt/sources.list.d/*.list; do
fileTotal=0
echo "FILENAME: $f"
while read -r ENTRY ; do
echo "ENTRY: $ENTRY"
# Sanitize entry line by removing stuff between square brackets
ENTRY=$(echo "$ENTRY" | sed 's/\[.*\] //')
HOST=$(cut -d/ -f3 <<< "$ENTRY")
if [ "ppa.launchpad.net" = "$HOST" ]; then
USER=$(cut -d/ -f4 <<< "$ENTRY")
PPA=$(cut -d/ -f5 <<< "$ENTRY")
packageCount=$(awk '$1=="Package:" {if (a[$2]++ == 0) {system("dpkg -l "$2)}}' /var/lib/apt/lists/*"$USER"*"$PPA"*Packages 2>/dev/null | awk '/^ii/' | wc -l)
else
USER=$(cut -d/ -f3 <<< "$ENTRY")
PPA=$(cut -d/ -f4 <<< "$ENTRY")
packageCount=$(awk '$1=="Package:" {if (a[$2]++ == 0) {system("dpkg -l "$2)}}' /var/lib/apt/lists/*"$USER"*Packages 2>/dev/null | awk '/^ii/' | wc -l)
fi
((fileTotal+=packageCount))
printf '\t%s\n' "REPOSITORY: $USER/$PPA"
printf '\t%s\n' "$packageCount package(s) installed"
echo
done < <(grep -Po "(?<=^deb\s).*?(?=#|$)" "$f")
# unrecognizedCount=$(grep -Pv "(?=^deb\s)|(?=^#).*" "$f" | grep -Pc "[^\s]")
unrecognizedCount=0
while read -r ENTRY ; do
((unrecognizedCount++))
printf '\t%s\n' "Unrecognized entry: $ENTRY"
done < <(grep -Pv "(?=^deb\s)|(?=^#).*" "$f" | grep -P "[^\s]")
echo "$fileTotal packages installed"
echo "$unrecognizedCount entries unrecognized"
if [ "$fileTotal" -eq 0 ] && [ "$unrecognizedCount" -eq 0 ]; then
if [ "$1" == "--delete" ]; then
sudo rm "$f"
sudo rm "$f".*
echo "$f deleted"
else
echo "Deletable"
fi
fi
echo "----------------------------"
done
用法完全相同,只是--delete
选项仅在没有任何已安装软件包的条目和未识别的条目时才删除文件。空白行和注释行不算作条目。
即使我考虑了所有我能考虑的,这仍然是未经测试的、可以安全运行的脚本sudo
。请备份/etc/apt/sources.list.d
目录中的所有内容。