List of packages upgraded last time `pkg upgrade` was executed

Michael Gmelin freebsd at grem.de
Wed Jan 27 09:07:07 UTC 2021



On Wed, 27 Jan 2021 10:35:38 +0200
Peter Pentchev <roam at ringlet.net> wrote:

> On Wed, Jan 27, 2021 at 10:57:22AM +0900, Yasuhiro Kimura wrote:
> > From: Freddie Cash <fjwcash at gmail.com>
> > Subject: Re: List of packages upgraded last time `pkg upgrade` was
> > executed Date: Tue, 26 Jan 2021 17:26:29 -0800
> >   
> > > /var/log/messages and I think /var/log/daemon include the output
> > > of the pkg commands. If you have the log files backed up from the
> > > last time it was run, you could grep for pkg in those.
> > > 
> > > No idea if this info is also stored in the sqlite databases pkg
> > > uses.  
> > 
> > Thank you for reply. But my intention is to write shell script that
> > gets the list of upgraded packages and does something by using
> > it. Because of that the list need to be gotton without any user
> > interaction. So unfortunately your method is not applicable to my
> > case.  
> 
> Well, there is the option of running a pkg query before and after
> the upgrade and comparing the results... especially if you write it in
> a higher-level language than the shell, it Should Not Be Too Hard(tm)
> to figure out which packages have changed their version, what new
> ones have appeared, which ones have been removed...
> 
> But, yeah, it is certainly easy for me to suggest that somebody else
> write something "simple" :)
> 

This will give you a list of all packages that were updated/installed
last:

  pkg query -e %t=$(pkg query %t | sort -n | tail -n1) %n-%v

Explained:

$(pkg query %t | sort -n | tail -n1)
- Get timestamps of when packages were installed
- Sort numerically
- Take the last one (which is the latest)

Feed this into `pkg query %t=x %n-%v` which returns all packages
matching that latest timestamp (== when last set of packages were
installed) and outputs their name and version.

As far as I can tell, packages installed by the same pkg invocation run
share the same installation timestamp (I didn't check the pkg sources,
but that's what appears to be the case), that's why this works. This
resulting list won't include the pkg package itself in case it was
updated as part as the run. There might also be other multi-pass
scenarios of pkg I'm not aware of though.

In case you know when pkg upgrade was called last, you can simply feed
it the timestamp directly, like in:

  pkg query -e "%t>=1609545326" %n-%v

If you use a script to do upgrades, you could store the timestamp as
part of that and do something like this:

  touch /tmp/lastupgrade
  pkg upgrade
  # then, later:
  pkg query -e "%t>=$(stat -f %m /tmp/lastupgrade)" %n-%v
  

Cheers,
Michael

-- 
Michael Gmelin


More information about the freebsd-ports mailing list