forked from MestreLion/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-remove-old
More file actions
executable file
·83 lines (67 loc) · 1.95 KB
/
kernel-remove-old
File metadata and controls
executable file
·83 lines (67 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Remove old kernels but keep the first, the current and the previous
#FIXME: 2.6.38-8.xx is sorted *after* 2.6.38-16.xx
# must use a smarter numeric sorting
myname="${0##*/}"
verbose=1
keepfirst=1
keepprevious=1
message() { ((verbose)) && printf "%s\n" "$1"; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid option: $1" ; }
usage() {
cat <<-USAGE
Usage: $myname [options]
USAGE
if [[ "$1" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Remove old kernels but keep the first, the current and the previous
Options:
-h|--help - show this page.
-q|--quiet - do not print informative messages.
--remove-first - also remove first kernel
--remove-previous - also remove previous kernel
Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
case "$1" in
-q|--quiet ) verbose=0 ;;
--remove-first ) keepfirst=0 ;;
--remove-previous) keepprevious=0 ;;
-*) invalid "$1" ;;
* ) argerr "$1" ;;
esac
shift
done
export current=$(uname -r | cut -d- -f-2)
list=$(dpkg --list "linux-*" | grep -E "^ii linux-(headers|image)-[2-5]\." | cut -d' ' -f3 | grep -v "$current")
message "Removing all kernels but keeping:"
keep=()
if ((keepfirst)); then
first=$(head -n 1 <<< "$list" | cut -d- -f3,4)
keep+=(-e "$first")
message "First : $first"
fi
if ((keepprevious)); then
previous=$(tail -n 1 <<< "$list" | cut -d- -f3,4)
keep+=(-e "$previous")
message "Previous: $previous"
fi
if ((${#keep[@]})); then
keep+=(-v)
else
keep=('')
fi
message "Current : $current"
sudo apt-get purge $(grep "${keep[@]}" <<< "$list") &&
message "Done, success!" ||
echo "Opps, something went wrong, better check the output!"