Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script requires package "apt-show-versions" that will be installed if not present.
- # This script asks user to remove packages that don't belong to main ubuntu repository.
- # In other words , seeks for packages installed from all PPA installed , and asks the user if want to remove or not.
- # with this script you can remove some or all packages of a given PPA , and also in later part of script will ask
- # user if wants to remove PPA configuration forever from system.
- # This script also will search for orfan packages, packages than went removed from main Ubuntu repository , but
- # somehow is still installed on system. the user will have the option to delete those.
- #don't forget to add valid values in configuration part of the script.these values can be obtained doing a
- #apt-cache policy <package> , being the selected package , one from the main ubuntu repositories.
- #the values pre-configured in the script should work well for most cases.
- #from my /etc/apt/sources.list , deb http://pt.archive.ubuntu.com/ubuntu/ wily universe , the default value used ;
- # archive.ubuntu.com will match most of the cases , using mirrors from any other country. just ubuntu.com/ubuntu
- #or ubuntu.com would be a safe string choice too.
- # feedback , bugs or new implementations , please email ubuntu-cleanup@outlook.pt
- # Version 1 - 15/1/2016
- # Version 1.1 - 17/1/2016 - added log notices - install script dependencies / delete packages/PPA
- # / apt-get update to /var/log/syslog
- # improvement in function list_packages_and_prompt_user variable $extraction2 . was causing a bug in some cases. now fixed.
- function validate_userid_equal_0 {
- if [ "$(id -u)" != "0" ]; then
- echo "This script uses superuser privileges therefore must be run with sudo" 1>&2
- exit 1
- fi
- }
- function install_script_requirements {
- file1="/usr/bin/apt-show-versions"
- if [ ! -f $file1 ] ; then
- echo "requirements not met, installing : apt-show-versions"
- sleep 2
- apt-get install -y apt-show-versions && logger "ubuntu.cleanup.sh - Installed package apt-show-versions"
- fi
- }
- function Yy {
- read -p "Do you want to remove this package ? (y/n)" -n 1 -r && echo -e
- if [[ $REPLY =~ ^[Yy]$ ]]
- then
- apt-get -y purge $i && logger "ubuntu.cleanup.sh - Remove package $i from system - belonged to $extraction2 PPA"
- else
- continue
- fi
- }
- function clean_ppa {
- echo "the final part of the script is to clean ppa than you don't really want to use no more."
- repositories=(/etc/apt/sources.list.d/*.list)
- for f in "${repositories[@]}"; do
- echo "$f"
- read -p "Do you want to remove this ppa ? (y/n)" -n 1 -r && echo -e
- if [[ $REPLY =~ ^[Yy]$ ]]
- then
- sudo rm -f $f
- echo "$f removed !" && logger "ubuntu.cleanup.sh - Removed PPA $f from /etc/apt/sources.list.d/"
- else
- continue
- fi
- done
- }
- function list_packages_and_prompt_user {
- for i in $(dpkg --get-selections | awk '{print $1}') ; do
- echo $i
- extraction1=`apt-cache policy $i |grep -i http | awk '{print $2}' | sed -e 's/\/$//g'`
- extraction2=`grep -s -i "^deb[[:blank:]]*$extraction1" /etc/apt/sources.list.d/*.list | awk -F: '{print $1}'`
- z=`apt-cache policy $i |grep -i "$url" |grep "$value" |wc -l`
- if (( z == 0 )) ; then
- clear
- echo "###############"
- apt-cache policy $i
- echo "###############"
- echo "this package belongs to repository below :"
- if [ -z "$extraction2" ]; then
- echo "Could not find related repository"
- else
- echo $extraction2
- fi
- Yy
- fi
- done
- }
- function remove_no_longer_maintained_packages {
- echo "removing packages that are no longer maintained/ not exist in the repositories"
- number_of_not_existent_packges=`apt-show-versions | grep 'No available' | cut -d' ' -f 1 | wc -l`
- if (( number_of_not_existent_packges != 0 )) ; then
- echo "found $number_of_not_existent_packges packages"
- for i in `apt-show-versions | grep 'No available' | cut -d' ' -f 1` ; do echo -e $i ; Yy ; done
- else
- echo "no packages in this state found !"
- fi
- }
- function exit_script {
- echo "finishing with apt-get update ..."
- sudo apt-get update > /dev/null && logger "ubuntu.cleanup.sh - apt-get update done"
- echo "Script completed !"
- exit 1
- }
- ## editable configuration ##
- url=archive.ubuntu.com
- value=500
- ## end of config ##
- validate_userid_equal_0
- install_script_requirements
- list_packages_and_prompt_user
- remove_no_longer_maintained_packages
- clean_ppa
- exit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement