debian.plugin.zsh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Use aptitude or apt if installed, fallback is apt-get
  2. # You can just set apt_pref='apt-get' to override it.
  3. if [[ -z $apt_pref || -z $apt_upgr ]]; then
  4. if [[ -e $commands[aptitude] ]]; then
  5. apt_pref='aptitude'
  6. apt_upgr='safe-upgrade'
  7. elif [[ -e $commands[apt] ]]; then
  8. apt_pref='apt'
  9. apt_upgr='upgrade'
  10. else
  11. apt_pref='apt-get'
  12. apt_upgr='upgrade'
  13. fi
  14. fi
  15. # Use sudo by default if it's installed
  16. if [[ -e $commands[sudo] ]]; then
  17. use_sudo=1
  18. fi
  19. # Aliases ###################################################################
  20. # These are for more obscure uses of apt-get and aptitude that aren't covered
  21. # below.
  22. alias age='apt-get'
  23. alias api='aptitude'
  24. # Some self-explanatory aliases
  25. alias acs="apt-cache search"
  26. alias aps='aptitude search'
  27. alias as="aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search"
  28. # apt-file
  29. alias afs='apt-file search --regexp'
  30. # These are apt-get only
  31. alias asrc='apt-get source'
  32. alias app='apt-cache policy'
  33. # superuser operations ######################################################
  34. if [[ $use_sudo -eq 1 ]]; then
  35. # commands using sudo #######
  36. alias aac="sudo $apt_pref autoclean"
  37. alias abd="sudo $apt_pref build-dep"
  38. alias ac="sudo $apt_pref clean"
  39. alias ad="sudo $apt_pref update"
  40. alias adg="sudo $apt_pref update && sudo $apt_pref $apt_upgr"
  41. alias adu="sudo $apt_pref update && sudo $apt_pref dist-upgrade"
  42. alias afu="sudo apt-file update"
  43. alias au="sudo $apt_pref $apt_upgr"
  44. alias ai="sudo $apt_pref install"
  45. # Install all packages given on the command line while using only the first word of each line:
  46. # acs ... | ail
  47. alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | xargs sudo $apt_pref install"
  48. alias ap="sudo $apt_pref purge"
  49. alias ar="sudo $apt_pref remove"
  50. alias aar="sudo $apt_pref autoremove"
  51. # apt-get only
  52. alias ads="sudo apt-get dselect-upgrade"
  53. # apt only
  54. alias alu="sudo apt update && apt list -u && sudo apt upgrade"
  55. # Install all .deb files in the current directory.
  56. # Warning: you will need to put the glob in single quotes if you use:
  57. # glob_subst
  58. alias dia="sudo dpkg -i ./*.deb"
  59. alias di="sudo dpkg -i"
  60. # Remove ALL kernel images and headers EXCEPT the one in use
  61. alias kclean='sudo aptitude remove -P "?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))"'
  62. # commands using su #########
  63. else
  64. alias aac="su -ls '$apt_pref autoclean' root"
  65. function abd() {
  66. cmd="su -lc '$apt_pref build-dep $@' root"
  67. print "$cmd"
  68. eval "$cmd"
  69. }
  70. alias ac="su -ls '$apt_pref clean' root"
  71. alias ad="su -lc '$apt_pref update' root"
  72. alias adg="su -lc '$apt_pref update && aptitude $apt_upgr' root"
  73. alias adu="su -lc '$apt_pref update && aptitude dist-upgrade' root"
  74. alias afu="su -lc '$apt-file update'"
  75. alias au="su -lc '$apt_pref $apt_upgr' root"
  76. function ai() {
  77. cmd="su -lc 'aptitude -P install $@' root"
  78. print "$cmd"
  79. eval "$cmd"
  80. }
  81. function ap() {
  82. cmd="su -lc '$apt_pref -P purge $@' root"
  83. print "$cmd"
  84. eval "$cmd"
  85. }
  86. function ar() {
  87. cmd="su -lc '$apt_pref -P remove $@' root"
  88. print "$cmd"
  89. eval "$cmd"
  90. }
  91. function aar() {
  92. cmd="su -lc '$apt_pref -P autoremove $@' root"
  93. print "$cmd"
  94. eval "$cmd"
  95. }
  96. # Install all .deb files in the current directory
  97. # Assumes glob_subst is off
  98. alias dia='su -lc "dpkg -i ./*.deb" root'
  99. alias di='su -lc "dpkg -i" root'
  100. # Remove ALL kernel images and headers EXCEPT the one in use
  101. alias kclean='su -lc "aptitude remove -P \"?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))\"" root'
  102. fi
  103. # Completion ################################################################
  104. #
  105. # Registers a compdef for $1 that calls $apt_pref with the commands $2
  106. # To do that it creates a new completion function called _apt_pref_$2
  107. #
  108. function apt_pref_compdef() {
  109. local f fb
  110. f="_apt_pref_${2}"
  111. eval "function ${f}() {
  112. shift words;
  113. service=\"\$apt_pref\";
  114. words=(\"\$apt_pref\" '$2' \$words);
  115. ((CURRENT++))
  116. test \"\${apt_pref}\" = 'aptitude' && _aptitude || _apt
  117. }"
  118. compdef "$f" "$1"
  119. }
  120. apt_pref_compdef aac "autoclean"
  121. apt_pref_compdef abd "build-dep"
  122. apt_pref_compdef ac "clean"
  123. apt_pref_compdef ad "update"
  124. apt_pref_compdef afu "update"
  125. apt_pref_compdef au "$apt_upgr"
  126. apt_pref_compdef ai "install"
  127. apt_pref_compdef ail "install"
  128. apt_pref_compdef ap "purge"
  129. apt_pref_compdef ar "remove"
  130. apt_pref_compdef aar "autoremove"
  131. apt_pref_compdef ads "dselect-upgrade"
  132. # Misc. #####################################################################
  133. # print all installed packages
  134. alias allpkgs='aptitude search -F "%p" --disable-columns ~i'
  135. # Create a basic .deb package
  136. alias mydeb='time dpkg-buildpackage -rfakeroot -us -uc'
  137. # Functions #################################################################
  138. # create a simple script that can be used to 'duplicate' a system
  139. function apt-copy() {
  140. print '#!/bin/sh'"\n" > apt-copy.sh
  141. cmd='$apt_pref install'
  142. for p in ${(f)"$(aptitude search -F "%p" --disable-columns \~i)"}; {
  143. cmd="${cmd} ${p}"
  144. }
  145. print $cmd "\n" >> apt-copy.sh
  146. chmod +x apt-copy.sh
  147. }
  148. # Prints apt history
  149. # Usage:
  150. # apt-history install
  151. # apt-history upgrade
  152. # apt-history remove
  153. # apt-history rollback
  154. # apt-history list
  155. # Based On: https://linuxcommando.blogspot.com/2008/08/how-to-show-apt-log-history.html
  156. function apt-history() {
  157. case "$1" in
  158. install)
  159. zgrep --no-filename 'install ' $(ls -rt /var/log/dpkg*)
  160. ;;
  161. upgrade|remove)
  162. zgrep --no-filename $1 $(ls -rt /var/log/dpkg*)
  163. ;;
  164. rollback)
  165. zgrep --no-filename upgrade $(ls -rt /var/log/dpkg*) | \
  166. grep "$2" -A10000000 | \
  167. grep "$3" -B10000000 | \
  168. awk '{print $4"="$5}'
  169. ;;
  170. list)
  171. zgrep --no-filename '' $(ls -rt /var/log/dpkg*)
  172. ;;
  173. *)
  174. echo "Parameters:"
  175. echo " install - Lists all packages that have been installed."
  176. echo " upgrade - Lists all packages that have been upgraded."
  177. echo " remove - Lists all packages that have been removed."
  178. echo " rollback - Lists rollback information."
  179. echo " list - Lists all contains of dpkg logs."
  180. ;;
  181. esac
  182. }
  183. # Kernel-package building shortcut
  184. function kerndeb() {
  185. # temporarily unset MAKEFLAGS ( '-j3' will fail )
  186. MAKEFLAGS=$( print - $MAKEFLAGS | perl -pe 's/-j\s*[\d]+//g' )
  187. print '$MAKEFLAGS set to '"'$MAKEFLAGS'"
  188. appendage='-custom' # this shows up in $(uname -r )
  189. revision=$(date +"%Y%m%d") # this shows up in the .deb file name
  190. make-kpkg clean
  191. time fakeroot make-kpkg --append-to-version "$appendage" --revision \
  192. "$revision" kernel_image kernel_headers
  193. }
  194. # List packages by size
  195. function apt-list-packages() {
  196. dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n' | \
  197. grep -v deinstall | \
  198. sort -n | \
  199. awk '{print $1" "$2}'
  200. }