#!/bin/bash shopt -s dotglob declare -A leafs=([/]=1) mapfile -t dirs < <(pacman -Qlq | grep '/$' | sort) for (( i = 0; i < ${#dirs[*]}; i++ )); do dir=${dirs[i]} parent=${dir%/*/}/ if [[ ${leafs["$parent"]} ]]; then unset leafs["$parent"] continue fi leafs["$dir"]=1 done files=() for l in "${!leafs[@]}"; do t=("$l"*) [[ -e $t ]] && files+=("${t[@]}") done printf '%s\n' "${files[@]}" | grep -vFf <(pacman -Qlq | grep -v '/$')