clean out old kernel versions?

Dave Stevens geek at uniserve.com
Sun Mar 7 17:24:37 UTC 2021


On Sun, 7 Mar 2021 17:13:47 +0000
Colin Law <clanlaw at gmail.com> wrote:

> #!/bin/sh
> # removes all except latest and current running kernels
> # call with -s to simulate
> 
> ( \
> KERNEL_HIGHEST=$(dpkg -l 'linux-image-[0-9.]*-[0-9]*-[a-zA-Z0-9]*' |
> grep ^ii | awk '{print $2}' | sort -V | tail -n 1 | sed
> 's/^linux-image-\([0-9.]*-[0-9]*\)-.*$/\1/') ; \
> KERNEL_CURRENT=$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/") ; \
> sudo apt-get purge $1 $(dpkg -l 'linux-*-[0-9-]*' | grep ^ii | awk
> '{print $2}' | sed "/$KERNEL_CURRENT/d;/$KERNEL_HIGHEST/d" ) ; \
> )
> 

cat cleanup.sh
#!/bin/sh
# removes all except latest and current running kernels
# call with -s to simulate

( \
KERNEL_HIGHEST=$(dpkg -l 'linux-image-[0-9.]*-[0-9]*-[a-zA-Z0-9]*' |
grep ^ii | awk '{print $2}' | sort -V | tail -n 1 | sed
's/^linux-image-\([0-9.]*-[0-9]*\)-.*$/\1/') ; \
KERNEL_CURRENT=$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/") ; \
sudo apt-get purge $1 $(dpkg -l 'linux-*-[0-9-]*' | grep ^ii | awk
'{print $2}' | sed "/$KERNEL_CURRENT/d;/$KERNEL_HIGHEST/d" ) ; \
)

user at user-Satellite-A100 /boot $ ./cleanup.sh
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be
  executed --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l'
  command --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single
  continuous long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and
  flush the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
./cleanup.sh: 8: ./cleanup.sh:
s/^linux-image-\([0-9.]*-[0-9]*\)-.*$/\1/: not found Usage: awk [POSIX
or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or
GNU style options] [--] 'program' file ... POSIX
options:		GNU long options: (standard) -f
progfile		--file=progfile -F
fs			--field-separator=fs -v
var=val		--assign=var=val Short options:
GNU long options: (extensions)
-b			--characters-as-bytes
-c			--traditional
-C			--copyright
-d[file]		--dump-variables[=file]
-D[file]		--debug[=file] -e
'program-text'	--source='program-text' -E
file			--exec=file
-g			--gen-pot -h			--help
	-i includefile		--include=includefile
	-l library		--load=library
	-L[fatal|invalid]	--lint[=fatal|invalid]
	-M			--bignum
	-N			--use-lc-numeric
	-n			--non-decimal-data
	-o[file]		--pretty-print[=file]
	-O			--optimize
	-p[file]		--profile[=file]
	-P			--posix
	-r			--re-interval
	-S			--sandbox
	-t			--lint-old
	-V			--version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
	gawk '{ sum += $1 }; END { print sum }' file
	gawk -F: '{ print $1 }' /etc/passwd
./cleanup.sh: 11: ./cleanup.sh: {print $2}: not found
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
user at user-Satellite-A100 /boot $ ls -l | wc
    441    3962   31288
user at user-Satellite-A100 /boot $ 

I hardly ever use sed so don't really understand the issue there.

d

-- 
In modern fantasy (literary or governmental), killing people is the
usual solution to the so-called war between good and evil. My books are
not conceived in terms of such a war, and offer no simple answers to
simplistic questions.

----- Ursula Le Guin




More information about the ubuntu-users mailing list