如何使用'aptitude'查看特定日期安装的软件包?

有人知道在使用aptitude(或apt-get)时,是否有一种简单的方法可以按日期排序找到已安装的软件包列表吗?
我安装了一堆软件包来尝试新东西,但结果不理想。我想删除所有这些软件包,以释放一些磁盘空间。
我尝试过只查看下载的.deb文件列表,但这似乎是一种相对繁琐的方式(尽管它确实有效)。

3有史以来的第一个问题! - Liso
8个回答

很遗憾, dpkg(aptitude所依赖的软件包管理器)并没有特别保存安装软件包的日期,尽管有考虑添加此功能。然而,可以通过查看写入目录/var/lib/dpkg/info的文件的时间戳来找到安装日期。

4如果您使用这种方法,请确保仅检查*.list文件上的时间戳,因为其他文件都带有软件包日期。 - Dave

默认情况下,aptitude会将日志写入到/var/log/aptitude文件中。它生成的输出如下所示:
===============================================================================

Aptitude 0.8.12: log report
Sun, Oct  1 2021 23:59:59 +1300

  IMPORTANT: this log only lists intended actions; actions which fail
  due to dpkg problems may not be completed.

Will install 6 packages, and remove 0 packages.
31.3 MB of disk space will be used
========================================
[UPGRADE] libc-dev-bin:amd64 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
[UPGRADE] libc6:amd64 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
[UPGRADE] libc6:i386 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
[UPGRADE] libc6-dbg:amd64 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
[UPGRADE] libc6-dev:amd64 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
[UPGRADE] libc6-i386:amd64 2.31-0ubuntu9 -> 2.31-0ubuntu9.2
========================================

Log complete.
===============================================================================

这显示了aptitude安装的精确日期和软件包。要配置此项(在`/etc/apt/apt.conf`或`/etc/apt/apt.conf.d/`中的单独文件中),请按照aptitude手册中的配置文件参考进行操作(也可从菜单中的“帮助 > 用户手册”获取):
Option:Aptitude::Log

Default:/var/log/aptitude

Description: If this is set to a nonempty string, aptitude will log the package
installations, removals, and upgrades that it performs. If the value of
Aptitude::Log begins with a pipe character (ie, ``|''), the remainder of its
value is used as the name of a command into which the log will be piped: for
instance, |mail -s 'Aptitude install run' root will cause the log to be emailed
to root. To log to multiple files or commands, you may set this option to a list
of log targets.

2请发布链接和如何实施的方法。仅仅参考一些手册文本并不是很有帮助。 - not2qubit

有一种简单的方法可以查看所有软件包的安装日期。只需执行以下命令:
grep " install" /var/log/dpkg.log*

作为结果,您将获得一个包含所有已安装软件的列表,其中包括确切的日期和时间。
感谢您的评论,这些评论引导我找到了解决方案。

2如果您在“cat”命令中使用完整路径,那么“cd”命令就是不必要的。 - papukaija
1cd命令的目的是在此目录中使用ls命令来检查可用的dpkg.log文件。但更好的解决方案是运行ls /var/log | grep 'dpkg.log'以列出日志文件。对于造成的混乱,我表示抱歉。 - jmarceli
2还是只用 "grep install /var/log/dpkg.log*" 呢? - Marc Van Daele
1哎呀,我不知道cat | cat是一种可以做的事情。但为什么不在一个命令中同时使用cat两个文件呢?(或者更好的是:就像@MarcVanDaele说的那样去做。) - mwfearnley

我在网上找到了这个。它可以根据dpkg日志文件创建一个dpkg的历史记录。
看起来非常简单。
function apt-history(){
      case "$1" in
        install)
              cat /var/log/dpkg.log | grep 'install '
              ;;
        upgrade|remove)
              cat /var/log/dpkg.log | grep $1
              ;;
        rollback)
              cat /var/log/dpkg.log | grep upgrade | \
                  grep "$2" -A10000000 | \
                  grep "$3" -B10000000 | \
                  awk '{print $4"="$5}'
              ;;
        *)
              cat /var/log/dpkg.log
              ;;
      esac
}

来源

编辑

我在Ubuntu 8.10服务器上尝试了这个脚本,效果非常好。 你能提供一些信息,说明你是如何解决你的问题的吗?


  • 使用 dpkg 日志

    locate dpkg.log | xargs cat {} | grep " install "
    
  • 或者如果你没有 locate

    find /var/log/ -name 'dpkg.log' | xargs cat {} | grep " install "
    
  • 使用 sort 来确保按时间顺序排序

    locate dpkg.log | xargs cat {} | grep " install " | sort
    
  • 使用 tac(反向的 cat)*,head 例如获取最新的4个条目

    locate dpkg.log | xargs cat {} | grep " install " | sort | tac | head -n4
    
例如,对于最后一个命令,我得到:
2014-10-08 18:56:12 install xorg-server-source:all <none> 2:1.16.1-1
2014-10-08 18:49:34 install libelementary-data:all <none> 0.7.0.55225-1
2014-10-08 18:46:57 install e17:i386 <none> 0.17.6-1
2014-10-08 18:46:56 install libedje-bin:i386 <none> 1.8.6-2.1+b1

1为什么你要使用 tac + head 而不是 tail - Zanna
1好久不见,我忘记了为什么——但可能有一个完全合理的原因……或者可能是我脑子一时糊涂 :D - a20

你还可以通过检查/var/log/apt/term.log以及较旧的文件term.log.1.gz等来追踪你之前的操作。它包含了安装过程中的消息和完整日志,带有时间戳。

确实有一个“官方”的 pkginstall.sh 脚本可以完成这个任务。请按照官方文档中的说明进行操作。简单来说,从上面的链接下载脚本,确保它可执行,然后运行以下命令:

~/pkginstalls.sh

这将在您的主目录中创建一个名为pkginstalls.txt的文件,其中包含按日期排序的所有已安装软件包。
顺便说一下,这是脚本的内容:
#!/bin/bash
#pkginstalls.sh
#creates text file with a list of all packages installed by date

#first append all info from archived logs

i=2
mycount=$(ls -l /var/log/dpkg.log.*.gz | wc -l)
nlogs=$(( $mycount + 1 ))

while [ $i -le $nlogs ]
do
if [ -e /var/log/dpkg.log.$i.gz ]; then
zcat /var/log/dpkg.log.$i.gz | grep "\ install\ " >> $HOME/pkgtmp.txt
fi
i=$(( $i+1 ))

done

#next append all info from unarchived logs

i=1
nulogs=$(ls -l /var/log/dpkg.log.* | wc -l)
nulogs=$(( $nulogs - $nlogs + 1 ))
while [ $i -le $nulogs ]
do
if [ -e /var/log/dpkg.log.$i ]; then
cat /var/log/dpkg.log.$i | grep "\ install\ " >> $HOME/pkgtmp.txt
fi
i=$(( $i+1 ))

done

#next append current log

cat /var/log/dpkg.log | grep "\ install\ " >> $HOME/pkgtmp.txt

#sort text file by date

sort -n $HOME/pkgtmp.txt > $HOME/pkginstalls.txt

rm $HOME/pkgtmp.txt

exit 0

[回答实际问题],是的,有一种简单的方法可以查找安装在特定日期的软件包,即使是在终端中使用apt-get安装的。
如果您安装了Synaptic软件包管理器,它可以免费从Ubuntu软件中心安装,您只需要打开其“文件”菜单并选择“历史记录”选项。在那里,您将找到所有添加和删除的应用程序包的记录,按日期组织,无论它们是如何安装或删除的。