Use awk
to parse apt
history log and filter it.
Python is better suited for this task, but the following awk
code will help you get started.
#!/bin/bash # Parse apt history log - shorter version # https://sleeplessbeastie.eu/2021/06/18/how-to-parse-apt-history-log/ cat /var/log/apt/history.log | awk -v PACKAGE=$1 ' BEGIN { RS="\n\n" FS="\n" printf "%20s %10s %50s %60s\n", \ "Datetime", "Action", "Package", "Version" } { # Sample entry for reference # # Start-Date: 2021-04-24 13:53:19 # Commandline: apt install vim # Requested-By: vagrant (1000) # Install: vim:amd64 (2:8.2.2434-3), libgpm2:amd64 (1.20.7-8, automatic), vim-runtime:amd64 (2:8.2.2434-3, automatic) # End-Date: 2021-04-24 13:53:20 # Create an object using key, value pairs for(field = 1; field <= NF; field++) { separator_index = index($field, ":") key = substr($field, 0, separator_index - 1) value = substr($field, separator_index + 2) switch(key) { case "Start-Date": case "End-Date": case "Commandline": case "Requested-By": object[key] = value break case "Remove": case "Purge": case "Install": case "Reinstall": case "Upgrade": case "Downgrade": object["Action"] = key object["Packages"] = value break } } # Filter by package name split(object["Packages"], packages, /), /) # Referential elements: # vim:amd64 (2:8.2.2434-3 # libgpm2:amd64 (1.20.7-8, automatic # vim-runtime:amd64 (2:8.2.2434-3, automatic) for(package_index in packages) { split(packages[package_index], package_entry_array,/ \(/) gsub(/\)/,"", package_entry_array[2]) split(package_entry_array[1], package_with_arch_array, ":") if(package_with_arch_array[1] ~ PACKAGE || PACKAGE == "") { printf "%20s %10s %50s %60s\n", \ object["Start-Date"], tolower(object["Action"]), package_with_arch_array[1], package_entry_array[2] } } } '
Display apt
history.
$ inspect_apt_history
Datetime Action Package Version 2021-04-19 19:24:18 install libtimedate-perl 2.3300-2, automatic 2021-04-19 19:24:18 install libhtml-tagset-perl 3.20-4, automatic 2021-04-19 19:24:18 install mariadb-server 1:10.5.9-1 2021-04-19 19:24:18 install libdbi-perl 1.643-3+b1, automatic [...] 2021-04-24 06:54:20 upgrade apt-listchanges 3.23, 3.24 2021-04-24 06:54:23 upgrade sysvinit-utils 2.96-6, 2.96-7 2021-04-24 06:54:25 upgrade dpkg 1.20.7.1, 1.20.9 2021-04-24 13:53:19 install vim 2:8.2.2434-3 2021-04-24 13:53:19 install libgpm2 1.20.7-8, automatic 2021-04-24 13:53:19 install vim-runtime 2:8.2.2434-3, automatic
Display apt
history related to vim
package.
$ inspect_apt_history vim
Datetime Action Package Version 2021-04-21 21:23:27 upgrade vim-common 2:8.2.2434-1, 2:8.2.2434-3 2021-04-21 21:23:27 upgrade vim-tiny 2:8.2.2434-1, 2:8.2.2434-3 2021-04-24 13:53:19 install vim 2:8.2.2434-3 2021-04-24 13:53:19 install vim-runtime 2:8.2.2434-3, automatic
Display apt
history related to mariadb
package.
$ inspect_apt_history mariadb
Datetime Action Package Version 2021-04-19 19:24:18 install mariadb-server 1:10.5.9-1 2021-04-19 19:24:18 install libdbd-mariadb-perl 1.21-3, automatic 2021-04-19 19:24:18 install mariadb-server-core-10.5 1:10.5.9-1, automatic 2021-04-19 19:24:18 install mariadb-client-10.5 1:10.5.9-1, automatic 2021-04-19 19:24:18 install mariadb-common 1:10.5.9-1, automatic 2021-04-19 19:24:18 install libmariadb3 1:10.5.9-1, automatic 2021-04-19 19:24:18 install mariadb-client 1:10.5.9-1 2021-04-19 19:24:18 install mariadb-server-10.5 1:10.5.9-1, automatic 2021-04-19 19:24:18 install mariadb-client-core-10.5 1:10.5.9-1, automatic
Display apt
history related to the exact mariadb
package.
$ inspect_apt_history ^mariadb$
Datetime Action Package Version 2021-04-19 19:24:18 install mariadb-server 1:10.5.9-1
Modify this script to display more information from apt
history.
$ inspect_apt_history linux-image
Datetime Action Package Version and command 2021-04-21 21:22:40 upgrade linux-image-amd64 5.10.13-1, 5.10.28-1 /usr/bin/unattended-upgrade
There are several limitations, but now you should be able to create your own solution or extend this script.