3

I have these kernel versions installed which via cron reboot job have been "touched" with the last access date:

/boot$ ll vmlinuz*
-rw------- 1 root root 5836336 Jan  8 20:00 vmlinuz-3.13.0-92-generic
-rw------- 1 root root 5017584 Oct 18 13:28 vmlinuz-3.2.0-113-generic
-rw------- 1 root root 7069136 Jan 25 16:58 vmlinuz-4.4.0-59-generic
-rw------- 1 root root 7070992 Feb  8 19:38 vmlinuz-4.4.0-62-generic
-rw------- 1 root root 7087088 Feb 21 04:26 vmlinuz-4.4.0-63-generic
-rw------- 1 root root 7087152 Feb 20 06:40 vmlinuz-4.4.0-64-generic
-rw------- 1 root root 7087024 Mar  3 11:25 vmlinuz-4.4.0-66-generic
-rw------- 1 root root 6988624 Nov 19 21:01 vmlinuz-4.4.33-040433-generic
-rw------- 1 root root 7046080 Jun 24  2016 vmlinuz-4.6.3-040603-generic
-rw------- 1 root root 3974752 Aug 16  2016 vmlinuz-4.7.1-040701-generic
-rw------- 1 root root 4134688 Aug 20  2016 vmlinuz-4.7.2-040702-generic
-rw------- 1 root root 4134688 Sep  7  2016 vmlinuz-4.7.3-040703-generic
-rw------- 1 root root 4138784 Jan  8 20:17 vmlinuz-4.7.5-040705-generic
-rw------- 1 root root 7431968 Nov 28 08:03 vmlinuz-4.8.10-040810-generic
-rw------- 1 root root 4994848 Oct  7 08:50 vmlinuz-4.8.1-040801-generic
-rw------- 1 root root 7415584 Jan  8 19:58 vmlinuz-4.8.11-040811-generic
-rw------- 1 root root 7431968 Jan  8 19:57 vmlinuz-4.8.12-040812-generic
-rw------- 1 root root 7427872 Oct 22 05:46 vmlinuz-4.8.4-040804-generic
-rw------- 1 root root 7427872 Nov 19 11:24 vmlinuz-4.8.5-040805-generic
-rw------- 1 root root 7485216 Jan  2 15:12 vmlinuz-4.9.0-040900-generic
-rw------- 1 root root 7419680 Feb 24 04:26 vmlinuz-4.9.10-040910-generic
-rw------- 1 root root 7485216 Jan 10 04:15 vmlinuz-4.9.1-040901-generic
-rw------- 1 root root 7419680 Mar  5 17:40 vmlinuz-4.9.12-040912-generic
-rw------- 1 root root 7419680 Mar  8 04:16 vmlinuz-4.9.13-040913-generic
-rw------- 1 root root 7403296 Jan 25 18:21 vmlinuz-4.9.4-040904-generic
-rw------- 1 root root 7403296 Feb  2 17:14 vmlinuz-4.9.5-040905-generic
-rw------- 1 root root 7419680 Feb 12 00:43 vmlinuz-4.9.8-040908-generic
-rw------- 1 root root 7415584 Feb 12 10:58 vmlinuz-4.9.9-040909-generic

For a given kernel I'd like to add up the file size to show how much space can be saved by deleting that kernel. For example 4.7.1 is ancient history in computer terms and at End of Life (EOL):

/boot$ ll *4.7.1*
-rw-r--r-- 1 root root  1238700 Aug 16  2016 abi-4.7.1-040701-generic
-rw-r--r-- 1 root root   181872 Aug 16  2016 config-4.7.1-040701-generic
-rw-r--r-- 1 root root 41705644 Feb  9 16:50 initrd.img-4.7.1-040701-generic
-rw------- 1 root root  3141159 Aug 16  2016 System.map-4.7.1-040701-generic
-rw------- 1 root root  3974752 Aug 16  2016 vmlinuz-4.7.1-040701-generic

What would be the best way of creating a list / array of:

Kernel Version w.x.y-zzzz - Last Access - Size
Kernel Version w.x.y-zzzz - Last Access - Size
    .     .     .      .      .     .      .
Kernel Version w.x.y-zzzz - Last Access - Size

The plan is to present this list using zenity with option to delete specific entries from the partition to save space. I install new kernels once or twice a week (they no longer come out every Sunday like they used to) so my 30 GB partition needs pruning every two or three months.

muru
  • 197,895
  • 55
  • 485
  • 740

3 Answers3

2

Use du to get the total size and stat to get the access time:

#! /bin/bash
for f in /boot/vmlinuz*
do
    [[ $f =~ vmlinuz-(.*)-generic ]]
    v=${BASH_REMATCH[1]}
    s=$(du -ch /boot/*-$v-* | awk '/total/{print $1}')
    printf '%s - %s - %s\n' "$v" "$(stat -c %x "$f")" "$s"
done

Output:

4.4.0-21 - 2016-12-24 16:20:59.858505053 +0900 - 48M
4.4.0-57 - 2016-12-24 16:58:03.000000000 +0900 - 48M
4.4.0-59 - 2017-01-11 10:41:35.000000000 +0900 - 48M
4.4.0-62 - 2017-02-03 13:25:58.000000000 +0900 - 48M
4.4.0-64 - 2017-02-23 20:07:03.105502000 +0900 - 48M
4.4.0-66 - 2017-03-08 10:24:16.000000000 +0900 - 48M
muru
  • 197,895
  • 55
  • 485
  • 740
  • Definitely the closest answer so far. Feeding this to Zenity radio list will give a nice user friendly script for removing older kernels. I would remove seconds and time zone. I would convert to 12 hour time format for readability and sort by date for code portability to other applications. – WinEunuuchs2Unix Mar 09 '17 at 11:07
  • @WinEunuuchs2Unix stat can also print the Unix timestamp, which you can then use with date to gate whichever date format you need. – muru Mar 09 '17 at 11:10
  • Yes I learned that last weekend on another project with stack overflow question I posted: http://stackoverflow.com/questions/42521666/convert-todays-hhmm-am-pm-to-epoch-in-bash – WinEunuuchs2Unix Mar 09 '17 at 11:15
  • Sorry for delay in testing code and accepting answer. Works perfectly and illustrates elaborate bash techniques! – WinEunuuchs2Unix Mar 10 '17 at 01:45
1

Here's a Rube Goldberg version, in case you are looking for something to dissect in a script...

ls -l|grep "4.8" |awk '{print $5}'|paste -s -d+ |bc
James
  • 1,083
  • please check you code, the output isn't as expected! – George Udosen Mar 09 '17 at 05:07
  • there isn't anything wrong with the code, but there are a few assumptions here. For starters, which directory you are in. And, that you want to match line items with "4.8", and you haven't weirdly aliased "ls", and so on. You might get an error if you skip the "grep" because of the total from ls turning into a blank line that blows up "bc". It was just meant to be a quick example for re-purposing and dissecting. If you have a specific error or question about what it does (or is doing wrong for you), I'd be happy to follow up. – James Mar 09 '17 at 05:57
  • Ok I see, didn't take note of the ls -l part and please could you add these explanation to your answer so others like me will know straight away how it should be used. – George Udosen Mar 09 '17 at 07:28
  • just ran this in the /boot folder and this was the output 175093460 – George Udosen Mar 09 '17 at 07:31
  • I copy and pasted the one liner into the terminal (whilst in /boot) and got 355620829 (355.6 MB). To make code human readable append |numfmt --to=iec-i --suffix=B --padding=7 to end. – WinEunuuchs2Unix Mar 09 '17 at 10:50
0

This is what the du command is designed for. Just do du -s *4.7.1* and it will give you the size (loop this for each installed kernel version). You can also use flags like -h or -k to change the output format. See man du for all of the possibilities.