24

Since the installation, I have added not too few PPAs to my 15.04 system because I wanted a specific application. But sometimes I have removed that package later again, because it did not work, was not what I really wanted, or just because I don't need it any more. I don't know if that makes a difference, but some repositories were installed with add-apt-repository while others just came by a command that just created the file in /etc/apt/sources.list.d/ manually.

Now how can I check whether I have any packages from a PPA installed and if not, remove it from my software sources? This procedure should automatically work through all PPAs.

2
  • As standalone script or as an extension for y-ppa-manager? ; )
    – A.B.
    Sep 17, 2015 at 5:41
  • @A.B. Standalone, please. I don't use y-ppa-manager.
    – Byte Commander
    Sep 17, 2015 at 8:11

3 Answers 3

31

Here is a script. Without a parameter, the script lists some infos. With --delete, the list files will be removed, if no packages are installed.

#!/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

Copy the code above in a new file, e.g.:

mkdir -p ~/bin
nano ~/bin/checkPPAs

Make the script executable

chmod +x ~/bin/checkPPAs

And run it with

~/bin/checkPPAs

to list all repositories and the number of installed packages.

Start the script with

~/bin/checkPPAs --delete

to remove the list files.


Sample output on my system

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
11
  • Why do you think removing them is a bad idea? And how would you preferably remove it? add-apt-repository --remove? Or manually rm the ???.list file? What about GPG keys used by this PPA, will they also be removed if I use add-apt-repository --remove?
    – Byte Commander
    Sep 17, 2015 at 8:19
  • If no package is installed, then the file can be safely deleted. I will be adding. Remove the key is a bit more complicated.
    – A.B.
    Sep 17, 2015 at 8:42
  • I have updated my script.
    – A.B.
    Sep 17, 2015 at 8:46
  • 1
    Wait. This reads the list files and iterates over each entry. So if any of the entries has 0 packages installed it deletes the entire file? If so, this is horribly unsafe to use. Am I missing something?
    – timuçin
    Dec 13, 2022 at 9:46
  • 1
    @timuçin I see your proposed edit to the answer, but it changes it substantially enough that I think you should write up a separate answer (with credit and correction to this one) detailing your concerns and solution. If someone else can review and confirm your edit, then great, but I think it's more likely to get traction as a separate answer. Dec 19, 2022 at 4:11
7

I don't know of an automatic way, but this method is fairly quick:

  1. Start synaptic package manager and input your password when prompted

  2. In the left column toward the bottom, click on Origin

All the ppas on your system will now be listed at the top of the left column. You can click down the list and see if any software is installed from each. (if there is, there will be a green box by the package name in the right column). If there are many packages from the ppa (too many to see on the screen at once), you can click on the S at the top of the right column to sort them by status.

NOTE: If you have any unused ppas:

  1. Click Settings in the top menu
  2. Then Repositories
  3. Software and Updates window will be displayed.
  4. From this window you can remove the unused ppas from the Other Software tab.
2
  • He asked how to detect unused PPAs. There are already a zillion directions about how to delete a PPA via Software Update.
    – Joe C
    Sep 3, 2019 at 14:18
  • @JoeC I'm not sure what you are complaining about or why you downvoted my answer. It may not be the best way, but I do show how to detect unused PPAs. Sep 3, 2019 at 15:03
2

After seeing the accepted answer having a crucial error, I had to fix it and post it here.

A.B's answer iterates over the entries in each list file and if ANY of the entries in that file has 0 packages installed on the system, regardless of how many entries have packages, it removes the entire file.

It also completely disregards any entry that doesn't match a predefined format. It will not take them into account.

Also the script works with the entries matched. It leaves the empty files untouched.

So I fixed the script like so:

#!/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

The usage is exactly the same except --delete option only deletes a file if there's no entry with any installed packages and no unrecognized entries. Blank and commented lines don't count as entries.

Even having considered everything I can, this is still not tested, safe script to run with sudo privileges. Please backup everything in the /etc/apt/sources.list.d directory.

5
  • 1
    You should suggest an edit if you're trying to fix an existing answer.
    – muru
    Dec 13, 2022 at 12:17
  • you're right. i'll delete this answer (if i can) after suggesting the edit.
    – timuçin
    Dec 14, 2022 at 15:17
  • * after the edit is approved
    – timuçin
    Dec 14, 2022 at 15:30
  • 2
    @timuçin Lol - You can't win on this one, can you? I saw your edit, but I suggested it should be it's own answer. Dec 19, 2022 at 21:14
  • @muru I saw timuçin's edit but I passed on it, as I felt that (a) it changed the original post by too much for comfort, and (b) I didn't have the ability (or time) to confirm it myself. However, it appears that ultimately it was rejected by two other reviewers. I'd be curious what you think after looking at the edit. Should we push for approval of the change to the existing answer, or leave this as a new answer? Dec 19, 2022 at 21:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .