I install and test a lot of applications in my Linux VMs. Once done with the testing, I will remove the installed packages that are no longer used. Since I install many applications often, I find it bit time consuming task to manually remove packages one by one. Also I sometimes forget to uninstall the unnecessary packages. If you're anything like me, here is a simple workaround to find and remove all packages installed on a certain date and time in Linux.
As you may already know, we can find the list of installed packages sorted by installation date in various Linux distributions using any one of the following commands.
On Arch Linux, EndeavourOS and Manjaro Linux:
$ expac --timefmt='%Y-%m-%d %T' '%l\t%n'|sort -n
On Fedora, RHEL, CentOS, AlmaLinux and Rocky Linux:
$ sudo rpm -qa --last
On Debian, Ubuntu, Linux Mint, and Pop OS:
$ grep " install " /var/log/dpkg.log
The above commands will list all packages installed on all dates. However, we are interested to find and delete packages installed at a specific date and time only.
Remove Packages Installed On Certain Date
We can use %yyyy-%mm-%dd
parameter with grep command to list all packages installed on a specific date and time.
For example, the following command displays all installed packages on September 14, 2021 in my Debian 11 bullseye system.
$ grep "2021-09-14.*.install " /var/log/dpkg.log
Sample output:
2021-09-14 06:51:51 install tmpreaper:amd64 <none> 1.6.14+nmu2 2021-09-14 11:14:14 install hello:amd64 <none> 2.10-2 2021-09-14 11:17:13 install hello:amd64 <none> 2.10-2 2021-09-14 11:17:13 install tmpreaper:amd64 <none> 1.6.14+nmu2 2021-09-14 11:22:29 install hello:amd64 <none> 2.10-2 2021-09-14 11:22:29 install tmpreaper:amd64 <none> 1.6.14+nmu2 2021-09-14 11:41:16 install nano:amd64 5.4-2 5.4-2 2021-09-14 11:41:17 install tmpreaper:amd64 <none> 1.6.14+nmu2 2021-09-14 11:41:17 install vim-runtime:all 2:8.2.2434-3 2:8.2.2434-3 2021-09-14 11:41:18 install vim:amd64 2:8.2.2434-3 2:8.2.2434-3
As you see in the above output, I've installed tmpreaper, vim and nano packages.
If log rotation is enabled, the contents of /var/log/dpkg.log
file will be deleted. In that case, you can view the previous install log file using command:
$ grep "2021-09-14.*.install " /var/log/dpkg.log.1
If you want to list only the package names, excluding the install date/time and architecture details from the output, run:
$ grep "2021-09-14.*.install " /var/log/dpkg.log | awk '{ print $4 }' | cut -d: -f1
You will now see only the installed package names on the given date:
tmpreaper hello hello tmpreaper hello tmpreaper nano tmpreaper vim-runtime vim
We've now got the name of all installed packages on the given date.
To uninstall all packages installed on a certain date, simply add the respective apt
command arguments with xargs
at the end of the previous command.
Please note that the following commands will not ask any user confirmation before deleting the packages. So it is always a good practice to perform a dry run to verify which packages will be removed before running the actual command.
To simulate (dry run) the process of package removal, simply add -s
or --simulate
like below.
$ grep "2021-09-14.*.install " /var/log/dpkg.log | awk '{ print $4 }' | cut -d: -f1 | xargs sudo apt purge -y -s
The above command will only simulate the package removal process, but will not delete the packages.
Once you find out which packages are going to be removed, re-run the above command without -s
or --simulate
option:
$ grep "2021-09-14.*.install " /var/log/dpkg.log | awk '{ print $4 }' | cut -d: -f1 | xargs sudo apt purge -y
If log rotation is enabled, run this command instead:
$ grep "2021-09-14.*.install " /var/log/dpkg.log.1 | awk '{ print $4 }' | cut -d: -f1 | xargs sudo apt purge -y
This time the packages installed on the specified date will be removed from your system.
1 comment
Hi,
Thanks a lot
Very nice article