-5

The end goal is reduce the footprint that apps and their dependencies use. To that end, we need to know the sizes of packages and their dependencies.

Unfortunately as any experienced Linux user will tell you, there is no safe way to automatically remove dependencies i.e., through a script. Inevitably if you do this you will break something. So you have to go about it manually.

Hence the user needs some way to find out what is taking up disk space because mere package size is insufficient. I have scoured the web looking for a command to provide this information but did not find one, so I constructed my own.

Zanna
  • 70,465
  • 2
    What exactly is it you’re trying to achieve? Maybe an example could shed more light on that. – dessert Apr 10 '20 at 20:48

2 Answers2

4

However, it is along the lines that automatically removing dependencies is risky business. So if you want to do it yourself then this tool answers an important question: What.

Focus on the results that you are trying to achieve: reducing the risk of automatically removing dependencies.

List the dependencies and reverse dependencies for a package with apt-cache depends <package-name> and apt-cache rdepends <package-name>. These are basic commands which can be tailored to different specific situations as follows.

  • The package names following apt-cache can be either a single package name or a list of multiple package names.

  • Pipe to the results of apt-cache commands to less with | less to return only the first part of the results if the results would be too long to be easy to read.

  • Install deborphan with sudo apt install deborphan and run deborphan to show a list of packages orphaned after uninstalling some packages. These orphaned packages are no longer necessary except if you are still using any of the packages that you just uninstalled. Unfortunately deborphan reports manually installed .deb packages that weren't installed via your normal software sources as orphaned packages too, so deborphan should be used together with apt-cache to make sure that you don't uninstall any packages that you still need even though deborphan reports them as orphaned packages.

Using a smart combination of all these commands will save a lot of time compared to reading a long list all packages, their sizes, their dependencies and the sizes thereof, by focusing only on the results that you need. Using these commands it is possible to clean up the list of installed packages after upgrading Ubuntu to a newer release in just a few minutes. For this particular task I also like to use Synaptic Package Manager because of its user-friendly graphical interface and powerful package filtering features.

karel
  • 114,770
-2

Note that the aptitude package is required for this.

for z in $(dpkg -l | awk '/^[hi]i/{print $2}' | grep -v '^lib'); do \
printf "\n$z:" && \
aptitude show $z | grep -E 'Uncompressed Size' && \
printf "\n" && \
apt show 2>/dev/null $(aptitude search '!~i?reverse-depends("^'$z'$")' -F "%p" | \
sed 's/:i386$//') | grep -E 'Package|Installed-Size' | sed '/APT/d;s/^.*Package:/\t&/;N;s/\n/ /'; done 
Zanna
  • 70,465