#!/usr/bin/env bash ######################################################## # # Prints a line with the files that can be removed # from PORTDIR # # Author: Fernando J. Pereda # License: Do with it whatever you want # Usage: # Execute it to retrieve a list of files that are # not claimed by any package installed. This list # may be used using bash command expansion feature: # # $ sudo rm $(distfiles-cleanable) # ######################################################## # # Revision History: # # 0.1: (25 August 2005) # -- Initial release # 0.2: (25 August 2005) # -- Fix ' ' in environment.bz2 # 0.3: (25 August 2005) # -- Exclude *-src directories from the list # 0.4: (26 August 2005) # -- Fix excluding *-src directories (thanks Narf) # ######################################################## # Get paths cache_vdb_path=$(portageq vdb_path) cache_portdir=$(portageq portdir) # Retrieve a list of files in distfiles/ distfiles_list=$(echo ${cache_portdir}/distfiles/*) # Now get a list of the files claimed by packages for e in ${cache_vdb_path}/*-*/*/environment.bz2 ; do claimed_list="${claimed_list} $(bzcat ${e} | sed -n -e "/^AA='\?\([^']*\)'\?$/s--\1-p")" done ##### # The first two attempts are the 'correct' way to solve this # particular problem; but the third one is way faster :) ##### ##### First attempt # Now check each file from distfiles_list to see if # it's in claimed_list #for d in ${distfiles_list} ; do # # Is the file claimed ? If so, remove it from the list # [[ -z ${claimed_list##*${d}*} ]] && distfiles_list=${distfiles_list//${d}} #done ##### #### Second attempt # Now remove every item in claimed_list from distfiles_list #for d in ${claimed_list} ; do # distfiles_list=${distfiles_list//${d}} #done #### #### Third attempt (aka let's hack) # Build a list of sed expressions: -e s/filename// for each filename in claimed_list sed_args=$(sed -e "s~\([^ ]*\)\( \|$\)~-e s/\1// ~g" <<< ${claimed_list}) # Now call sed with that list, remove cache_portdir prefix and remove extra spaces sed ${sed_args} \ -e 's~[^- ]*-src\( \|$\)~~g' \ -e "s~${cache_portdir}/distfiles.~~g" \ -e 's~ \+~ ~g' \ <<< ${distfiles_list} ####