How to sort APT `gnome-core` package dependencies by installed size?
Jul 27, 12:37am

To sort the dependencies of the gnome-core package by their installed size, you can use a combination of apt-cache and dpkg-query commands in the terminal. Here's a step-by-step guide:


Step 1: List Dependencies

First, list all the dependencies of the gnome-core package using apt-cache:

bash
apt-cache depends gnome-core

This will output a list of dependencies, including their types (e.g., Depends, Recommends, Suggests).


Step 2: Filter and Extract Package Names

Extract only the package names from the dependencies. You can use grep and awk to filter and format the output:

bash
apt-cache depends gnome-core | grep -E "Depends|Recommends|Suggests" | awk '{print $2}'

This will give you a list of package names that gnome-core depends on.


Step 3: Get Installed Size for Each Dependency

Use dpkg-query to get the installed size of each package. The installed size is stored in the Installed-Size field of the package metadata.

bash
dpkg-query -W -f='${Installed-Size}\t${Package}\n' <package-name>

To apply this to all dependencies, you can loop through the list of packages:

bash
for pkg in $(apt-cache depends gnome-core | grep -E "Depends|Recommends|Suggests" | awk '{print $2}'); do
    dpkg-query -W -f='${Installed-Size}\t${Package}\n' "$pkg" 2>/dev/null
done

This will output the installed size (in kilobytes) and the package name for each dependency.


Step 4: Sort by Installed Size

Finally, sort the output by the installed size in descending order:

bash
for pkg in $(apt-cache depends gnome-core | grep -E "Depends|Recommends|Suggests" | awk '{print $2}'); do
    dpkg-query -W -f='${Installed-Size}\t${Package}\n' "$pkg" 2>/dev/null
done | sort -nr

This will display the dependencies sorted by their installed size, from largest to smallest.


Full Command

Here’s the full command to achieve this in one line:

bash
for pkg in $(apt-cache depends gnome-core | grep -E "Depends|Recommends|Suggests" | awk '{print $2}'); do dpkg-query -W -f='${Installed-Size}\t${Package}\n' "$pkg" 2>/dev/null; done | sort -nr

Notes:

  1. 2>/dev/null: This suppresses errors for packages that are not installed or do not exist.
  2. sort -nr: This sorts the output in reverse numerical order (largest first).
  3. Installed Size: The size is in kilobytes (KB). To convert to megabytes (MB), divide by 1024.

Jul 27, 12:37am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more