Useful Oneliners - Decluttering Ubuntu Server

Here's a useful oneliner I cooked up recently to help me clean up a long-running Ubuntu Server:

diff -y <(dpkg-query -f '${binary:Package}\n' -W) <(curl https://releases.ubuntu.com/24.04/ubuntu-24.04.3-live-server-amd64.manifest | cut -f 1 - )

It shows what packages are installed locally that weren't in the original install manifest.

Some of them may be needed or added by upgrades, but quite a number may have been added surreptitiously, and simply stuck around.

Especially useful for server systems, where userspace should be as clean as possible and many userspace things should run in containers.

The side-by-side diff (installed vs. manifest) will look something like:

adduser                            adduser
amd64-microcode                    amd64-microcode
apparmor                           apparmor
apport                             apport
apport-core-dump-handler           apport-core-dump-handler
apport-symptoms                    apport-symptoms
appstream                          appstream
apt                                apt
apt-utils                          apt-utils
attr                               <
base-files                         base-files
base-passwd                        base-passwd
bash                               bash
bash-completion                    bash-completion
bc                                 bc
                                    > bcache-tools
bind9-dnsutils                     bind9-dnsutils
bind9-host                         bind9-host
bind9-libs:amd64                   bind9-libs:amd64
binutils                           | bolt
binutils-common:amd64              <
binutils-x86-64-linux-gnu          <
bpfcc-tools                        bpfcc-tools
bpftrace                           bpftrace
bsdextrautils                      bsdextrautils
bsdutils                           bsdutils
btop                               <
btrfs-progs                        btrfs-progs
build-essential                    <
busybox-initramfs                  busybox-initramfs
busybox-static                     busybox-static
byobu                              byobu
bzip2                              <
ca-certificates                    ca-certificates
                                    > casper
                                    > cifs-utils
cloud-guest-utils                  cloud-guest-utils
                                    > cloud-init
cloud-initramfs-copymods           cloud-initramfs-copymods
cloud-initramfs-dyn-netconf        cloud-initramfs-dyn-netconf
command-not-found                  command-not-found
...

I can then go through and remove the packages from the left side that don't need to be there anymore.

Even more useful is to throw a grep < on the end of there and see only the extra installed packages.

diff -y <(dpkg-query -f '${binary:Package}\n' -W) <(cat ubuntu-24.04.3-live-server-amd64.manifest | cut -f 1 - ) | grep '<'

attr                                  <
binutils-common:amd64                 <
binutils-x86-64-linux-gnu             <
btop                                  <
build-essential                       <
bzip2                                 <
containerd.io                         <
cpp                                   <
cpp-11                                <
cpp-13                                <
cpp-13-x86-64-linux-gnu               <
cpp-x86-64-linux-gnu                  <
dialog                                <
dns-root-data                         <
docker-buildx-plugin                  <
docker-ce                             <
docker-ce-cli                         <
docker-ce-rootless-extras             <
docker-compose-plugin                 <
dos2unix                              <
dpkg-dev                              <
exiv2                                 <
fakeroot                              <
g++-13-x86-64-linux-gnu               <
g++-x86-64-linux-gnu                  <
gcc                                   <
gcc-11                                <
gcc-11-base:amd64                     <
gcc-13                                <
gcc-13-base:amd64                     <
gcc-13-x86-64-linux-gnu               <
gcc-x86-64-linux-gnu                  <

After removing the packages, they'll still show up in dpkg -l as rc "removed but config files still in place",

$ dpkg -l | grep ^rc
rc  powertop                              2.15-3build1                            amd64        diagnose issues with power consumption and management
rc  silversearcher-ag                     2.2.0+git20200805-1.1                   amd64        very fast grep-like program, alternative to ack-grep
rc  usbmuxd                               1.1.1-5~exp3ubuntu2.1                   amd64        USB multiplexor daemon for iPhone and iPod Touch devices

You can purge them completely as follows:

$ sudo apt purge $(dpkg -l | grep "^rc" | awk '{print $2}')