1. Overview

Sometimes we install packages in Linux and then realize that they aren’t exactly what we’re looking for. We can easily remove unwanted packages using package managers and specifying the package names.

However, there are times when we’ve installed many packages with dependencies that we can’t remember. In this tutorial, we’ll learn two ways for listing and removing the latest packages installed on our Linux machine.

2. Using rpm

rpm is among the most popular package managers used by many repositories like RHEL, CentOS, Fedora, and OpenSUSE. It’s also used by yum and some other package manager utilities.

2.1. Listing Installed Packages Using rpm

We can get a list of installed packages with the rpm command using the -q option for query and the –a option as its select option, to get all installed packages:

$ rpm -qa
audit-2.7.6-3.el7.x86_64
centos-release-7-4.1708.el7.centos.x86_64
tuned-2.8.0-5.el7.noarch
setup-2.8.71-7.el7.noarch
chrony-3.1-2.el7.centos.x86_64
basesystem-10.0-7.el7.centos.noarch
qemu-guest-agent-2.8.0-2.el7.x86_64
bind-license-9.9.4-50.el7.noarch
openssh-server-7.4p1-11.el7.x86_64
firewalld-filesystem-0.4.4.4-6.el7.noarch
...

To show the results in reverse order of installation, based on dates, we can use the –last option:

$ rpm -qa --last
iwl6000-firmware-9.221.4.1-56.el7.noarch Thu 27 Jul 2023 05:24:04 AM EDT
iwl3945-firmware-15.32.2.9-56.el7.noarch Thu 27 Jul 2023 05:24:04 AM EDT
iwl2000-firmware-18.168.6.1-56.el7.noarch Thu 27 Jul 2023 05:24:04 AM EDT
iwl105-firmware-18.168.6.1-56.el7.noarch Thu 27 Jul 2023 05:24:04 AM EDT
iwl7265-firmware-22.0.7.0-56.el7.noarch Thu 27 Jul 2023 05:24:03 AM EDT
rootfiles-8.1-11.el7.noarch Thu 27 Jul 2023 05:24:01 AM EDT
...

2.2. Removing the Latest Packages

As we want to remove the first N packages, we need the first N lines of the previous command’s output. Let’s first save all the output lines to a file:

$ rpm -qa --last > package_list

Then, we can open the package_list file with a text editor like Vim or Nano and remove extra lines. Also, we can remove the extra lines with commands such as sed. Finally, we have a file that only contains the package list that we want to remove.

Now, we can send package names to the rpm or any higher-level package manager utility like yum to uninstall them:

$ sudo rpm -e $(awk '{print $1}' <package_list)

We used awk to extract the first section of lines (package names) and send them to the rpm command for removal operation. A successful uninstall produces no output. However, if any package needs the one we’re removing, the above command fails and returns an error.

Notably, it’s better to uninstall the packages with yum because it considers dependencies and does remove operations with more checks while rpm is low-level and only removes the specified packages:

$ sudo yum remove $(awk '{print $1}' <package_list)

3. Using dpkg

Debian-based distributions of Linux use dpkg, the Debian package manager, for managing packages. As dpkg has no tool to list installed packages by the installation date, we can use its logs to see when the packages are installed.

3.1. Listing Installed Packages Using dpkg Logs

Let’s first take a look at dpkg logs:

$ ls /var/log/dpkg.log*
/var/log/dpkg.log    /var/log/dpkg.log.2.gz  /var/log/dpkg.log.4.gz  /var/log/dpkg.log.6.gz
/var/log/dpkg.log.1  /var/log/dpkg.log.3.gz  /var/log/dpkg.log.5.gz  /var/log/dpkg.log.7.gz

In most Debian-based Linux distributions, dpkg logs are stored in a rotational format, and old logs are compressed.

In our case, to list all installed packages, we can search these log files using the zgrep tool. zgrep invokes grep on compressed or gzipped files and all options specified will be passed directly to the grep command.

Let’s find all lines in the log files that are related to the package installation by finding the ‘install ‘ word. To hide file names at the start of the lines, we can use the -h option:

$ zgrep -h 'install ' /var/log/dpkg.log*
2023-05-01 16:57:37 install zsh-common:all <none> 5.7.1-1+deb10u1
2023-05-01 16:57:43 install zsh:armhf <none> 5.7.1-1+deb10u1
2023-04-24 21:53:12 install tcl8.6:armhf <none> 8.6.9+dfsg-2
2022-10-05 14:52:55 install libcolorhug2:armhf <none> 1.4.3-4
...

3.2. Removing the Latest Packages Using dpkg -r Option

To remove the latest packages, we can sort the above list in descending order using the Linux sort command with the -r option.

It’s worth noting that lines start with the date and they are in the YYYY-MM-DD format. So, the result of the alphabetic sort is in descending order based on date and time. Therefore, like in the previous section, we can save the list in a file, and cut it from where we want or we can use pipe.

Let’s remove the latest package using the dpkg command in one line using pipes:

$ sudo dpkg -r $(zgrep -h 'install ' /var/log/dpkg.log* | sort -r | head -n 1 | awk '{print $4}')
(Reading database ... 105178 files and directories currently installed.)
Removing zsh (5.7.1-1+deb10u1) ...
Processing triggers for man-db (2.8.5-2) ...

In the previous command, after searching for the ‘install ‘ word with zgrep, we sort the result and get the latest line with the head command. Then, to access the package name, we used awk and get the fourth portion of the log line, which is the package name, and pass the result to dpkg to remove it. The -r option of dpkg is for removing the specified package.

Although dpkg removes the packages, it doesn’t remove the related logs from the log files. So, if we use zgrep to search the ‘install ‘ phrase again, we’ll get the same result as before.

Therefore, to solve this problem, we can also search the ‘remove ‘ phrase and then, find the last packages that we’ve installed but not removed. Notably, if we pass the removed package’s names to dpkg for removal, it returns an error and ignores them.

4. Conclusion

In this article, we discussed listing and removing the latest packages installed in our Linux operating system. We took a look at two of the most used package managers.

First, we used the rpm -q option to find installed packages with installation timestamps and then, removed the last ones using either the rpm or yum command.

Later, we searched in the dpkg logs to collect a list of the last added package names and then, removed them with the dpkg -r option.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.