Last active
October 6, 2025 06:22
-
-
Save renhiyama/74087477547cc85fff91b30f05c4f5af to your computer and use it in GitHub Desktop.
Get a list of all dependencies, their sizes & how many apps use it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Colors for pretty output | |
| GREEN="\033[0;32m" | |
| YELLOW="\033[1;33m" | |
| RESET="\033[0m" | |
| declare -A deps_map | |
| declare -A app_seen | |
| get_binaries_from_desktop_files() { | |
| local desktop_dirs=( | |
| "/usr/share/applications" | |
| "/usr/local/share/applications" | |
| "$HOME/.local/share/applications" | |
| ) | |
| local binaries=() | |
| for dir in "${desktop_dirs[@]}"; do | |
| [[ -d "$dir" ]] || continue | |
| while IFS= read -r line; do | |
| local exec_path | |
| exec_path=$(echo "$line" | sed -E 's/^Exec=([^ ]+).*/\1/' | sed 's/^"//;s/"$//') | |
| [[ -n "$exec_path" ]] && binaries+=("$exec_path") | |
| done < <(grep -h "^Exec=" "$dir"/*.desktop 2>/dev/null || true) | |
| done | |
| printf "%s\n" "${binaries[@]}" | sort -u | |
| } | |
| get_binary_path() { | |
| local binary="$1" | |
| local path | |
| path=$(which "$binary" 2>/dev/null || true) | |
| if [[ -z "$path" ]]; then | |
| echo "⚠️ Skipping $binary (not found)" >&2 | |
| return 1 | |
| fi | |
| echo "$path" | |
| } | |
| collect_deps() { | |
| local binary="$1" | |
| local libs | |
| libs=$(ldd "$binary" 2>/dev/null | awk '{print $3}' | grep '^/' || true) | |
| for lib in $libs; do | |
| deps_map["$lib"]+="$binary " | |
| done | |
| } | |
| human_size() { | |
| local size=$1 | |
| local units=(B KiB MiB GiB) | |
| local unit_index=0 | |
| while (( size > 1024 && unit_index < 3 )); do | |
| size=$(( size / 1024 )) | |
| ((unit_index++)) | |
| done | |
| echo "${size}${units[$unit_index]}" | |
| } | |
| main() { | |
| local binaries=("$@") | |
| if [[ ${#binaries[@]} -eq 0 ]]; then | |
| echo -e "${YELLOW}No inputs specified, scanning .desktop files...${RESET}" | |
| mapfile -t binaries < <(get_binaries_from_desktop_files) | |
| fi | |
| echo -e "${GREEN}Analyzing ${#binaries[@]} applications...${RESET}" | |
| for app in "${binaries[@]}"; do | |
| local path | |
| path=$(get_binary_path "$app") || continue | |
| [[ -n "${app_seen[$path]+_}" ]] && continue | |
| app_seen["$path"]=1 | |
| collect_deps "$path" | |
| done | |
| echo | |
| echo -e "${YELLOW}Shared Library Usage (by number of apps):${RESET}" | |
| echo "-------------------------------------------------------------" | |
| printf "%6s %-10s %s\n" "Count" "Size" "Library" | |
| echo "-------------------------------------------------------------" | |
| for lib in "${!deps_map[@]}"; do | |
| local unique_count | |
| unique_count=$(echo "${deps_map[$lib]}" | tr ' ' '\n' | sort -u | wc -l) | |
| local size="N/A" | |
| if [[ -f "$lib" ]]; then | |
| local bytes | |
| bytes=$(stat -c%s "$lib" 2>/dev/null || echo 0) | |
| size=$(numfmt --to=iec-i --suffix=B "$bytes" 2>/dev/null || human_size "$bytes") | |
| fi | |
| printf "%6d %-10s %s\n" "$unique_count" "$size" "$lib" | |
| done | sort -k1,1nr -k2,2h | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment