Advertisement
Guest User

ubuntu-cleanup.sh

a guest
Jan 17th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.31 KB | None | 0 0
  1. #!/bin/bash
  2. # This script requires package "apt-show-versions" that will be installed if not present.
  3. # This script asks user to remove packages that don't belong to main ubuntu repository.
  4. # In other words , seeks for packages installed from all PPA installed , and asks the user if want to remove or not.
  5. # with this script you can remove some or all packages of a given PPA , and also in later part of script will ask
  6. # user if wants to remove PPA configuration forever from system.
  7. # This script also will search for orfan packages, packages than went removed from main Ubuntu repository , but
  8. # somehow is still installed on system. the user will have the option to delete those.
  9. #don't forget to add valid values in configuration part of the script.these values can be obtained doing a
  10. #apt-cache policy <package> , being the selected package , one from the main ubuntu repositories.
  11. #the values pre-configured in the script should work well for most cases.
  12. #from my /etc/apt/sources.list , deb http://pt.archive.ubuntu.com/ubuntu/ wily universe , the default value used ;
  13. # archive.ubuntu.com will match most of the cases , using mirrors from any other country. just ubuntu.com/ubuntu
  14. #or ubuntu.com would be a safe string choice too.
  15. # feedback , bugs or new implementations , please email ubuntu-cleanup@outlook.pt
  16. # Version 1 - 15/1/2016
  17. # Version 1.1 - 17/1/2016 - added log notices - install script dependencies / delete packages/PPA
  18. # / apt-get update to /var/log/syslog
  19. # improvement in function list_packages_and_prompt_user variable $extraction2 . was causing a bug in some cases. now fixed.
  20.  
  21. function validate_userid_equal_0 {
  22. if [ "$(id -u)" != "0" ]; then
  23.     echo "This script uses superuser privileges therefore must be run with sudo" 1>&2
  24.     exit 1
  25. fi
  26. }
  27.  
  28. function install_script_requirements {
  29.  
  30. file1="/usr/bin/apt-show-versions"
  31.  
  32. if [ ! -f $file1 ] ; then
  33.     echo "requirements not met, installing : apt-show-versions"
  34.     sleep 2
  35.     apt-get install -y apt-show-versions && logger "ubuntu.cleanup.sh - Installed package apt-show-versions"
  36. fi
  37. }
  38.  
  39. function Yy {
  40.     read -p "Do you want to remove this package ? (y/n)" -n 1 -r && echo -e
  41.  
  42. if [[  $REPLY =~ ^[Yy]$ ]]
  43.     then
  44.         apt-get -y purge $i && logger "ubuntu.cleanup.sh - Remove package $i from system - belonged to $extraction2 PPA"
  45.     else
  46.         continue
  47. fi
  48. }
  49.  
  50. function clean_ppa {
  51. echo "the final part of the script is to clean ppa than you don't really want to use no more."
  52. repositories=(/etc/apt/sources.list.d/*.list)
  53. for f in "${repositories[@]}"; do
  54.    echo "$f"
  55.     read -p "Do you want to remove this ppa ? (y/n)" -n 1 -r && echo -e
  56.  
  57. if [[  $REPLY =~ ^[Yy]$ ]]
  58.     then
  59.         sudo rm -f $f
  60.         echo "$f removed !" && logger "ubuntu.cleanup.sh - Removed PPA $f from /etc/apt/sources.list.d/"
  61.     else
  62.         continue
  63. fi
  64. done
  65. }
  66.  
  67.  
  68. function list_packages_and_prompt_user {
  69. for i in $(dpkg --get-selections | awk '{print $1}') ; do
  70.     echo $i
  71. extraction1=`apt-cache policy $i |grep -i http | awk '{print $2}' | sed -e 's/\/$//g'`
  72. extraction2=`grep -s -i "^deb[[:blank:]]*$extraction1" /etc/apt/sources.list.d/*.list | awk -F: '{print $1}'`
  73. z=`apt-cache policy $i |grep -i "$url" |grep "$value" |wc -l`
  74. if (( z == 0 )) ; then
  75. clear
  76. echo "###############"
  77. apt-cache policy $i
  78. echo "###############"
  79. echo "this package belongs to repository below :"
  80. if [ -z "$extraction2" ]; then
  81.     echo "Could not find related repository"
  82. else
  83.     echo $extraction2
  84. fi
  85. Yy
  86. fi
  87. done
  88. }
  89.  
  90. function remove_no_longer_maintained_packages {
  91. echo "removing packages that are no longer maintained/ not exist in the repositories"
  92. number_of_not_existent_packges=`apt-show-versions | grep 'No available' | cut -d' ' -f 1 | wc -l`
  93. if (( number_of_not_existent_packges != 0 )) ; then
  94.     echo "found $number_of_not_existent_packges packages"
  95.     for i in `apt-show-versions | grep 'No available' | cut -d' ' -f 1` ; do  echo -e $i ; Yy ; done
  96. else
  97.         echo "no packages in this state found !"
  98. fi
  99. }
  100.  
  101. function exit_script {
  102. echo "finishing with apt-get update ..."
  103. sudo apt-get update > /dev/null && logger "ubuntu.cleanup.sh - apt-get update done"
  104. echo "Script completed !"
  105. exit 1
  106. }
  107.  
  108. ## editable configuration ##
  109. url=archive.ubuntu.com
  110. value=500
  111. ## end of config ##
  112.  
  113. validate_userid_equal_0
  114. install_script_requirements
  115. list_packages_and_prompt_user
  116. remove_no_longer_maintained_packages
  117. clean_ppa
  118. exit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement