Skip to content

Instantly share code, notes, and snippets.

@ubuntuvibes
Created April 24, 2017 09:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubuntuvibes/0e184045d78d9da5fad7f2d6fe88558c to your computer and use it in GitHub Desktop.
Save ubuntuvibes/0e184045d78d9da5fad7f2d6fe88558c to your computer and use it in GitHub Desktop.
# source: https://askubuntu.com/a/675109
# 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"
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)
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=$(awk '$1=="Package:" {if (a[$2]++ == 0) {system("dpkg -l "$2)}}' /var/lib/apt/lists/*"$USER"*Packages 2>/dev/null | awk '/^ii/' | wc -l)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment