[pacman-dev] [PATCH] pacsysclean: Add new contrib script

Dave Reisner d at falconindy.com
Sun Aug 7 20:16:01 EDT 2011


On Sun, Aug 07, 2011 at 05:14:06PM -0400, Eric Bélanger wrote:
> pacsysclean sort installed packages by decreasing installed size. It's
> useful for finding large unused package when doing system clean-up. This
> script is an improved version of other similar scripts posted on the
> forums. Thanks goes to Dave as I reused the size_to_human function from his
> paccache script.
> 
> Signed-off-by: Eric Bélanger <snowmaniscool at gmail.com>
> 
> ---
> 
> If you can think of a better name, feel free to suggest one.
> ---
>  contrib/.gitignore     |    1 +
>  contrib/Makefile.am    |    5 ++-
>  contrib/pacsysclean.in |   87 ++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 92 insertions(+), 1 deletions(-)
>  create mode 100755 contrib/pacsysclean.in
> 
> diff --git a/contrib/.gitignore b/contrib/.gitignore
> index 1bd145f..19b81e0 100644
> --- a/contrib/.gitignore
> +++ b/contrib/.gitignore
> @@ -6,5 +6,6 @@ paclist
>  paclog-pkglist
>  pacscripts
>  pacsearch
> +pacsysclean
>  wget-xdelta.sh
>  zsh_completion
> diff --git a/contrib/Makefile.am b/contrib/Makefile.am
> index 10b03a2..754096d 100644
> --- a/contrib/Makefile.am
> +++ b/contrib/Makefile.am
> @@ -5,7 +5,8 @@ OURSCRIPTS = \
>  	paclist \
>  	paclog-pkglist \
>  	pacscripts \
> -	pacsearch
> +	pacsearch \
> +	pacsysclean
>  
>  OURFILES = \
>  	bash_completion \
> @@ -21,6 +22,7 @@ EXTRA_DIST = \
>  	paclist.in \
>  	pacscripts.in \
>  	pacsearch.in \
> +	pacsysclean.in \
>  	vimprojects \
>  	zsh_completion.in \
>  	README
> @@ -59,6 +61,7 @@ paclist: $(srcdir)/paclist.in
>  paclog-pkglist: $(srcdir)/paclog-pkglist.in
>  pacscripts: $(srcdir)/pacscripts.in
>  pacsearch: $(srcdir)/pacsearch.in
> +pacsysclean: $(srcdir)/pacsysclean.in
>  pactree: $(srcdir)/pactree.in
>  zsh_completion: $(srcdir)/zsh_completion.in
>  
> diff --git a/contrib/pacsysclean.in b/contrib/pacsysclean.in
> new file mode 100755
> index 0000000..e393e24
> --- /dev/null
> +++ b/contrib/pacsysclean.in
> @@ -0,0 +1,87 @@
> +#!/bin/bash
> +
> +# pacsysclean - Sort installed packages by decreasing installed size. Useful for system clean-up.
> +#
> +# Copyright (C) 2011 Eric Bélanger <eric at archlinux.org>
> +#
> +# This program is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU General Public License
> +# as published by the Free Software Foundation; either version 2
> +# of the License, or (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +export TEXTDOMAIN='pacman'
> +export TEXTDOMAINDIR='/usr/share/locale'
> +
> +# determine whether we have gettext; make it a no-op if we do not
> +if ! type gettext &>/dev/null; then
> +	gettext() {
> +		echo "$@"
> +	}
> +fi
> +
> +usage() {
> +	echo "$0 - Sort installed packages by decreasing installed size."
> +	echo
> +	echo "Usage: $0 [options]"
> +	echo
> +	echo "Options:"
> +	echo "  -a               List all packages (Default)"
> +	echo "  -e               List unrequired explicitely installed packages"
> +	echo "  -h, --help       Show this help message and exit"
> +}
> +
> +size_to_human() {
> +	awk -v size="$1" '
> +	BEGIN {
> +		suffix[1] = "KiB"
> +		suffix[2] = "MiB"
> +		suffix[3] = "GiB"
> +		suffix[4] = "TiB"
> +		count = 1
> +
> +		while (size > 1024) {
> +			size /= 1024
> +			count++
> +		}
> +
> +		sizestr = sprintf("%.2f", size)
> +		sub(/.?0+$/, "", sizestr)
> +		printf("%s %s", sizestr, suffix[count])
> +	}'
> +}
> +
> +PACMAN_OPTS="-Qq"
> +if [ -n "$1" ]; then
> +	case "$1" in
> +		-a) PACMAN_OPTS="-Qq" ;;
> +		-e) PACMAN_OPTS="-Qetq" ;;
> +		-h|--help) usage; exit 0 ;;
> +		*) usage; exit 1 ;;
> +	esac
> +fi
> +
> +TEMPDIR=$(mktemp -d /tmp/cleanup-script.XXXX)
> +cd $TEMPDIR
> +
> +# Sort installed packages by decreasing installed size. Useful for system clean-up.
> +for package in $(pacman $PACMAN_OPTS); do
> +	echo $(pacman -Qi $package |grep 'Installed Size' |awk '{print $4}') $package
> +done | sort -g -r -o raw.txt
> +
> +N=0
> +while read LINE ; do
> +	N=$((N+1))
> +	size_to_human $(echo -n $LINE |cut -d' ' -f1) >> sorted-list.txt
> +	echo -n ' : ' >> sorted-list.txt
> +	echo -n $LINE |cut -d' ' -f2 >> sorted-list.txt
> +done < raw.txt
> +
> +echo "Files saved to $TEMPDIR"
> -- 
> 1.7.6
> 

I don't like the idea of using temp files for this when stdout will
suffice (meaning the user can do with what they want. You can join all
this together:

  while read -r package; do
    printf '%s\t%s\n' "$(pacman -Qi $package | awk '/^Installed Size/{print $4}')" "$package"
  done | sort -gr | while IFS=$'\t' read -r size pkg; do
    printf '%s\t%s\n' "$pkg" "$(size_to_human "$size")"
  done


But really, this is why I wrote expac.... so all that pacman|awk
stuff turns into...

  expac '%n\t%m' | sort -rgk2 | while IFS=$'\t' read name size; do
    printf '%s\t%s\n' "$name" "$(size_to_human "${size% *}")"
  done

Obviously, not officially sanctioned...

dave



More information about the pacman-dev mailing list