Here is script which prints only the currently installed top level packages, where "top level packages" are defined as apt packages upon which no other apt packages depend. If such top level programs were installed by apt or a package manager such as Synaptic, then they were manually chosen by the user.
#!/bin/sh
NumDaysAgo=18
find /var/lib/dpkg/info -name "*.list" -mtime -$NumDaysAgo \
-exec stat -c $'%y\t%n' {} \; | \
sed -e 's,/var/lib/dpkg/info/,,' -e 's,\.list,,' | \
sort -r | \
while read Date Time Xxx Pkg
do
lncnt=$(apt-cache --installed rdepends $Pkg | wc -l)
if [ $lncnt -eq "2" ]
then echo "$Date $Time $Pkg"
fi
done
echo "JOB COMPLETED: $BASH_SOURCE"
The packages are printed in reverse order under the assumption that the user is more likely to want the newer info sooner, and because the program is slow.
Program flow:
- The program first gathers into a list all the installed packages by reading the filenames under
/var/lib/dpkg/info/
. The file mod times are the installation times.
- That list is sorted in reverse order.
- For each installed package
$Pkg
, a call to apt-cache rdepends $Pkg
requests the reverse-dependencies of $Pkg
. If there are no dependencies, then it is a top level package and the package info is printed: date time packagename
Notes:
- The script depends upon the output format of
apt-cache rdepends $Pkg
which was intended for human eyes and could change in the future versions of apt.
- The code for the part gathering filenames under /var/lib/dpkg/info/ came from
this unix.stackexchange post. As that poster 'mikel' pointed out, the
dpgk
history logfiles are not reliable because they will be rotated out after reaching a certain volume.
- Man page for
apt-cache
- The call
apt-cache rdepends ...
is very slow presumably because each call is computed by iterating through all the dependencies. Hence the above script starts from the newest installs to offer the user as much instant gratification as possible.
- The
--installed
flag after apt-cache
checks that the dpkg-installed packages are also apt-installed. If the user or another install software bypassed apt and used dpkg directly, it would be possible. THIS CASE HAS NOT BEEN TESTED, but I think something noticeable would be printed in either the standard or error output
- The output does not include manually chosen packages which later became depended upon by a higher package. The output can also include packages which were installed via apt by other third party install software, and hence are not truly manually installed. However, if the purpose of the output is as a basis for setting up a restored Linux from a backup
/home
directory which included said third party software, then this output would be suitable.
- Some of the package names include version numbers, and some do not. Mentioned just to bring awareness to the fact.
dpkg
in/var/log/dpkg.log*
.apt-get install
command to install a package, and I have provided a solution which gives this information unless the history file has been altered in certain ways (e.g.: the entries have been removed).