Advertisement
Guest User

ubuntu-cleanup.script

a guest
Jan 15th, 2016
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.75 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 dnfgomes@hotmail.com .
  16. # Version 1 - 15/1/2016
  17.  
  18. function validate_userid_equal_0 {
  19. if [ "$(id -u)" != "0" ]; then
  20.     echo "This script uses superuser privileges therefore must be run with sudo" 1>&2
  21.     exit 1
  22. fi
  23. }
  24.  
  25. function install_script_requirements {
  26.  
  27. file1="/usr/bin/apt-show-versions"
  28.  
  29. if [ ! -f $file1 ] ; then
  30.     echo "requirements not met, installing : apt-show-versions"
  31.     sleep 2
  32.     apt-get install -y apt-show-versions
  33. fi
  34. }
  35.  
  36. function Yy {
  37.     read -p "Do you want to remove this package ? (y/n)" -n 1 -r && echo -e
  38.  
  39. if [[  $REPLY =~ ^[Yy]$ ]]
  40.     then
  41.         apt-get -y purge $i
  42.     else
  43.         continue
  44. fi
  45. }
  46.  
  47. function clean_ppa {
  48. echo "the final part of the script is to clean ppa than you don't really want to use no more."
  49. repositories=(/etc/apt/sources.list.d/*.list)
  50. for f in "${repositories[@]}"; do
  51.    echo "$f"
  52.     read -p "Do you want to remove this ppa ? (y/n)" -n 1 -r && echo -e
  53.  
  54. if [[  $REPLY =~ ^[Yy]$ ]]
  55.     then
  56.         sudo rm -f $f
  57.         echo "$f removed !"
  58.     else
  59.         continue
  60. fi
  61. done
  62. }
  63.  
  64.  
  65. function list_packages_and_prompt_user {
  66. for i in $(dpkg --get-selections | awk '{print $1}') ; do
  67.     echo $i
  68. extraction1=`apt-cache policy $i |grep -i http | awk '{print $2}' | sed -e 's/\/$//g'`
  69. extraction2=`grep -s -i -r $extraction1 /etc/apt/sources.list.d/*.list |awk -F: '{print $1}'`
  70. z=`apt-cache policy $i |grep -i "$url" |grep "$value" |wc -l`
  71. if (( z == 0 )) ; then
  72. clear
  73. echo "###############"
  74. apt-cache policy $i
  75. echo "###############"
  76. echo "this package belongs to repository below :"
  77. if [ -z "$extraction2" ]; then
  78.     echo "Could not find related repository"
  79. else
  80.     echo $extraction2
  81. fi
  82. Yy
  83. fi
  84. done
  85. }
  86.  
  87. function remove_no_longer_maintained_packages {
  88. echo "removing packages that are no longer maintained/ not exist in the repositories"
  89. number_of_not_existent_packges=`apt-show-versions | grep 'No available' | cut -d' ' -f 1 | wc -l`
  90. if (( number_of_not_existent_packges != 0 )) ; then
  91.     echo "found $number_of_not_existent_packges packages"
  92.     for i in `apt-show-versions | grep 'No available' | cut -d' ' -f 1` ; do  echo -e $i ; Yy ; done
  93. else
  94.         echo "no packages in this state found !"
  95. fi
  96. }
  97.  
  98. function exit_script {
  99. echo "finishing with apt-get updating ..."
  100. sudo apt-get update > /dev/null
  101. echo "Script completed !"
  102. exit 1
  103. }
  104.  
  105. ## editable configuration ##
  106. url=archive.ubuntu.com
  107. value=500
  108. ## end of config ##
  109.  
  110. validate_userid_equal_0
  111. install_script_requirements
  112. list_packages_and_prompt_user
  113. remove_no_longer_maintained_packages
  114. clean_ppa
  115. exit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement