1 # Use aptitude or apt if installed, fallback is apt-get
2 # You can just set apt_pref='apt-get' to override it.
4 if [[ -z $apt_pref || -z $apt_upgr ]]; then
5 if [[ -e $commands[aptitude] ]]; then
7 apt_upgr='safe-upgrade'
8 elif [[ -e $commands[apt] ]]; then
17 # Use sudo by default if it's installed
18 if [[ -e $commands[sudo] ]]; then
22 # Aliases ###################################################################
23 # These are for more obscure uses of apt-get and aptitude that aren't covered
28 # Some self-explanatory aliases
29 alias acs="apt-cache search"
30 alias aps='aptitude search'
31 alias as="aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search"
34 alias afs='apt-file search --regexp'
37 # These are apt-get only
38 alias asrc='apt-get source'
39 alias app='apt-cache policy'
41 # superuser operations ######################################################
42 if [[ $use_sudo -eq 1 ]]; then
43 # commands using sudo #######
44 alias aac="sudo $apt_pref autoclean"
45 alias abd="sudo $apt_pref build-dep"
46 alias ac="sudo $apt_pref clean"
47 alias ad="sudo $apt_pref update"
48 alias adg="sudo $apt_pref update && sudo $apt_pref $apt_upgr"
49 alias adu="sudo $apt_pref update && sudo $apt_pref dist-upgrade"
50 alias afu="sudo apt-file update"
51 alias au="sudo $apt_pref $apt_upgr"
52 alias ai="sudo $apt_pref install"
53 # Install all packages given on the command line while using only the first word of each line:
56 alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | xargs sudo $apt_pref install"
57 alias ap="sudo $apt_pref purge"
58 alias aar="sudo $apt_pref autoremove"
61 alias ads="sudo apt-get dselect-upgrade"
64 alias alu="sudo apt update && apt list -u && sudo apt upgrade"
66 # Install all .deb files in the current directory.
67 # Warning: you will need to put the glob in single quotes if you use:
69 alias dia="sudo dpkg -i ./*.deb"
70 alias di="sudo dpkg -i"
72 # Remove ALL kernel images and headers EXCEPT the one in use
73 alias kclean='sudo aptitude remove -P "?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))"'
76 # commands using su #########
78 alias aac="su -ls '$apt_pref autoclean' root"
80 cmd="su -lc '$apt_pref build-dep $@' root"
84 alias ac="su -ls '$apt_pref clean' root"
85 alias ad="su -lc '$apt_pref update' root"
86 alias adg="su -lc '$apt_pref update && aptitude $apt_upgr' root"
87 alias adu="su -lc '$apt_pref update && aptitude dist-upgrade' root"
88 alias afu="su -lc '$apt-file update'"
89 alias au="su -lc '$apt_pref $apt_upgr' root"
91 cmd="su -lc '$apt_pref install $@' root"
96 cmd="su -lc '$apt_pref purge $@' root"
101 cmd="su -lc '$apt_pref autoremove $@' root"
105 # Install all .deb files in the current directory
106 # Assumes glob_subst is off
107 alias dia='su -lc "dpkg -i ./*.deb" root'
108 alias di='su -lc "dpkg -i" root'
110 # Remove ALL kernel images and headers EXCEPT the one in use
111 alias kclean='su -lc "aptitude remove -P \"?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))\"" root'
114 # Completion ################################################################
117 # Registers a compdef for $1 that calls $apt_pref with the commands $2
118 # To do that it creates a new completion function called _apt_pref_$2
120 function apt_pref_compdef() {
124 eval "function ${f}() {
126 service=\"\$apt_pref\";
127 words=(\"\$apt_pref\" '$2' \$words);
129 test \"\${apt_pref}\" = 'aptitude' && _aptitude || _apt
135 apt_pref_compdef aac "autoclean"
136 apt_pref_compdef abd "build-dep"
137 apt_pref_compdef ac "clean"
138 apt_pref_compdef ad "update"
139 apt_pref_compdef afu "update"
140 apt_pref_compdef au "$apt_upgr"
141 apt_pref_compdef ai "install"
142 apt_pref_compdef ail "install"
143 apt_pref_compdef ap "purge"
144 apt_pref_compdef aar "autoremove"
145 apt_pref_compdef ads "dselect-upgrade"
147 # Misc. #####################################################################
148 # print all installed packages
149 alias allpkgs='aptitude search -F "%p" --disable-columns ~i'
151 # Create a basic .deb package
152 alias mydeb='time dpkg-buildpackage -rfakeroot -us -uc'
155 # Functions #################################################################
156 # create a simple script that can be used to 'duplicate' a system
157 function apt-copy() {
158 print '#!/bin/sh'"\n" > apt-copy.sh
160 cmd='$apt_pref install'
162 for p in ${(f)"$(aptitude search -F "%p" --disable-columns \~i)"}; {
166 print $cmd "\n" >> apt-copy.sh
173 # apt-history install
174 # apt-history upgrade
176 # apt-history rollback
178 # Based On: https://linuxcommando.blogspot.com/2008/08/how-to-show-apt-log-history.html
179 function apt-history() {
182 zgrep --no-filename 'install ' $(ls -rt /var/log/dpkg*)
185 zgrep --no-filename $1 $(ls -rt /var/log/dpkg*)
188 zgrep --no-filename upgrade $(ls -rt /var/log/dpkg*) | \
189 grep "$2" -A10000000 | \
190 grep "$3" -B10000000 | \
191 awk '{print $4"="$5}'
194 zgrep --no-filename '' $(ls -rt /var/log/dpkg*)
198 echo " install - Lists all packages that have been installed."
199 echo " upgrade - Lists all packages that have been upgraded."
200 echo " remove - Lists all packages that have been removed."
201 echo " rollback - Lists rollback information."
202 echo " list - Lists all contains of dpkg logs."
207 # Kernel-package building shortcut
209 # temporarily unset MAKEFLAGS ( '-j3' will fail )
210 MAKEFLAGS=$( print - $MAKEFLAGS | perl -pe 's/-j\s*[\d]+//g' )
211 print '$MAKEFLAGS set to '"'$MAKEFLAGS'"
212 appendage='-custom' # this shows up in $(uname -r )
213 revision=$(date +"%Y%m%d") # this shows up in the .deb file name
217 time fakeroot make-kpkg --append-to-version "$appendage" --revision \
218 "$revision" kernel_image kernel_headers
221 # List packages by size
222 function apt-list-packages() {
223 dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n' | \
224 grep -v deinstall | \
226 awk '{print $1" "$2}'