LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   is there a way to list all packages by size? (https://www.linuxquestions.org/questions/slackware-14/is-there-a-way-to-list-all-packages-by-size-872616/)

prol 04-02-2011 04:27 PM

is there a way to list all packages by size?
 
pkgtool just lists it in alphabetical order.

Thanks

samac 04-02-2011 05:28 PM

ls -alS /var/log/packages | more

would be one way of listing all the packages that you have installed, one page at a time.

samac

prol 04-02-2011 05:38 PM

That doesnt work properly. I was looking for something that would sort according the the installed size or as slackware mentions "uncompressed package size".

fskmh 04-02-2011 05:44 PM

Code:

grep UNCOMPRESSED /var/log/packages/* | awk -F: '{print $3,$1}' | sort -rn

brixtoncalling 04-02-2011 06:52 PM

Make that sort -rh to sort in 'human-readable' form.

fskmh 04-02-2011 06:59 PM

The sizes are already in KiB, so -h is superfluous.

brixtoncalling 04-02-2011 07:14 PM

Quote:

Originally Posted by fskmh (Post 4311901)
The sizes are already in KiB, so -h is superfluous.

Not on my system, they aren't!

hello.freeman 04-02-2011 09:36 PM

fordfas

audriusk 04-03-2011 06:36 AM

Quote:

Originally Posted by fskmh (Post 4311861)
Code:

grep UNCOMPRESSED /var/log/packages/* | awk -F: '{print $3,$1}' | sort -rn

Since my locale is lt_LT.utf8, and in Lithuania (as in many European countries) comma is used instead of dot as a decimal separator, I needed to add LC_ALL=C right before sort -rh. I also modified this one-liner to avoid using grep (awk is able to do grepping by itself). Here's my result:

Code:

awk -F: '/UNCOMPRESSED/ {print $2,FILENAME}' /var/log/packages/* | LC_ALL=C sort -rh | less
Anyway, thanks for useful one-liner.

allend 04-03-2011 09:13 AM

My results suggest the first form is more efficient:
Code:

time grep UNCOMPRESSED /var/log/packages/* | awk -F: '{print $3,$1}' | sort -rh
Quote:

real 0m0.046s
user 0m0.020s
sys 0m0.018s
Code:

time awk -F: '/UNCOMPRESSED/ {print $2,FILENAME}' /var/log/packages/* | LC_ALL=C sort -rh
Quote:

real 0m0.193s
user 0m0.172s
sys 0m0.013s

prol 04-03-2011 09:31 AM

sort -rh, thats what I was looking for, never knew the command awk existed lol. I have a lot to learn. Thanks for all our help.

kjhambrick 04-03-2011 09:34 AM

Very Nice !

I turned the one-liner into a shell script with some options

Not sure how to or if I can attach a shell script so here's the code:
Code:

#!/bin/sh

PrgNam="`basename $0`"
DirNam="`dirname  $0`"

if [ "$DirNam" = "." ]
then
  if [ -f "$PrgNam" ]
  then
      DirNam="`pwd`"
  else
      DirNam="`whichone $PrgNam`"
      DirNam="`dirname $DirNam`"
  fi
elif [ "$DirNam" = ".." ]
then
  DirNam="`pwd`"
  DirNam="`dirname $DirNam`"
elif [ "`dirname $DirNam`" = ".." ]
then
  FooNam="`basename $DirNam`"
  DirNam="`pwd -P`"
  DirNam="`dirname $DirNam`/$FooNam"
elif [ "$DirNam" = "" ]              # should not happen ...
then                                  # see: man 3 dirname
  DirNam="`whichone $PrgNam`"
  DirNam="`dirname $DirNam`"
fi

WhichSize="U"
SortOrd="S"
SortArg="-k1 -rh"
PkgDir="/var/log/packages"
DoTotal=1
IgnoreCase=1
PkgLst=""

Usage ()
{
  ErrNum=$1
  shift

  [ $# -gt 0 ] && echo -e "\n$*\n"

  echo -e "usage:  $PrgNam [ Options ] [PkgREx1[:PkgREx2[:PkgREx3]]]"
  cat  <<Usage_EOF

  $PrgNam prints Sizes and BaseNames of installed Packages.
       
  Options Include:

  -d Dir - override Package Dir with 'Dir'( default '$PkgDir' )
  -c    - print Compressed Size instead of Uncompressed Size
  -u    - print Uncompressed Size( default )
  -s    - sort by Size Descending( default )
  -n    - sort by Package Name Ascending
  -r    - reverse sort order
  -i    - do NOT ignore case resolving Package Names( default - ignore case )
  -t    - do NOT print total size of all packages( default - print total )
  -h    - this listing

  $PrgNam does accept an optional list of Colon-Delimited Regular Expressions
  to limit output to the packages listed in the Regex.

  Example:

  print a listing of packages ending with 'SBo' or 'alien' or beginning
  with 'nvidia'

  Do NOT ignore case and sort by Package Name

  # $PrgNam -ni 'SBo\$:alien\$:^nvidia'

          50K compat32-tools-2.1-noarch-5alien
      10370K flash-player-plugin-10.3_d162-x86_64-2_SBo
      41984K gcc-4.5.2_multilib-x86_64-2alien
      25600K gcc-g++-4.5.2_multilib-x86_64-2alien
      16384K gcc-gfortran-4.5.2_multilib-x86_64-2alien
      76800K gcc-gnat-4.5.2_multilib-x86_64-2alien
      187392K gcc-java-4.5.2_multilib-x86_64-2alien
      10240K gcc-objc-4.5.2_multilib-x86_64-2alien
      267264K glibc-2.13_multilib-x86_64-3alien
      209920K glibc-i18n-2.13_multilib-x86_64-3alien
      11264K glibc-profile-2.13_multilib-x86_64-3alien
      19456K glibc-solibs-2.13_multilib-x86_64-3alien
        3072K glibc-zoneinfo-2.13_multilib-noarch-3alien
      644096K libreoffice-3.3.2-x86_64-1alien
        260K libvdpau-0.4.1-x86_64-1_SBo
      72704K nvidia-driver-260.19.29_2.6.37.4-x86_64-1_SBo
      12288K nvidia-kernel-260.19.29_2.6.37.4-x86_64-1_SBo
        710K pidgin-guifications-2.16-x86_64-1_SBo
        3276K tgif-QPL-4.2.2-x86_64-1_SBo
  # ======== ===================================
  # 1613130K Total Bytes in 19 Packages

  Do the same search with default Args ...

  # $PrgNam 'SBo\$:alien\$:^nvidia'

      644096K libreoffice-3.3.2-x86_64-1alien
      267264K glibc-2.13_multilib-x86_64-3alien
      209920K glibc-i18n-2.13_multilib-x86_64-3alien
      187392K gcc-java-4.5.2_multilib-x86_64-2alien
      76800K gcc-gnat-4.5.2_multilib-x86_64-2alien
      72704K nvidia-driver-260.19.29_2.6.37.4-x86_64-1_SBo
      41984K gcc-4.5.2_multilib-x86_64-2alien
      25600K gcc-g++-4.5.2_multilib-x86_64-2alien
      19456K glibc-solibs-2.13_multilib-x86_64-3alien
      16384K gcc-gfortran-4.5.2_multilib-x86_64-2alien
      12288K nvidia-kernel-260.19.29_2.6.37.4-x86_64-1_SBo
      11264K glibc-profile-2.13_multilib-x86_64-3alien
      10370K flash-player-plugin-10.3_d162-x86_64-2_SBo
      10240K gcc-objc-4.5.2_multilib-x86_64-2alien
        3276K tgif-QPL-4.2.2-x86_64-1_SBo
        3072K glibc-zoneinfo-2.13_multilib-noarch-3alien
        710K pidgin-guifications-2.16-x86_64-1_SBo
        260K libvdpau-0.4.1-x86_64-1_SBo
          50K compat32-tools-2.1-noarch-5alien
  # ======== ===================================
  # 1613130K Total Bytes in 19 Packages


Usage_EOF
 
  exit $ErrNum

}
while  getopts hcusnrtid: junk 2>/dev/null
do
  case $junk in
      d)    PkgDir="$OPTARG"
            ;;
      c)    WhichSize="C"
            ;;
      u)    WhichSize="U"
            ;;
      s)    SortOrd="S"
            SortArg="-k1 -rh"
            ;;
      n)    SortOrd="N"
            SortArg="-k2"
            ;;
      r)    if [ "$SortOrd" = "S" ]
            then
              SortArg="-k1 -h"
            else
              SortArg="-k2 -r"
            fi
            ;;
      t)    DoTotal=0
            ;;
      i)    IgnoreCase=0
            ;;
      h)    Usage 0
            ;;
      *)    Usage 1
            ;;
  esac
done

shift `expr $OPTIND - 1`

[ $# -gt 0 ] && PkgLst="$@"

gawk '
BEGIN {

  PrgNam    = "'"$PrgNam"'"    ""
  DirNam    = "'"$DirNam"'"    ""
  PkgLst    = "'"$PkgLst"'"    ""
  WhichSize  = "'"$WhichSize"'"  ""
  IgnoreCase = "'"$IgnoreCase"'" +0
  DoTotal    = "'"$DoTotal"'"    +0
  SortOrd    = "'"$SortOrd"'"    ""
 
  DoPkgLst = 0

  if ( PkgLst != "" )
  {
      N = split( PkgLst, FooAry, ":" )

      for ( i = 1 ; i <= N ; i ++ )
      {
        if (( ThePkg = Trim( FooAry[i] )) == "" )
            continue

        if ( IgnoreCase == 1 )
            ThePkg = Str2Regex( ThePkg )

        PkgAry[ ThePkg ] = ++DoPkgLst
      }
  }

  FS = ":"

  CSizeREx = "^COMPRESSED PACKAGE SIZE"  # 132K
  USizeREx = "^UNCOMPRESSED PACKAGE SIZE" # 660K

  SizeREx  = ( WhichSize == "U" ) ? USizeREx : CSizeREx

  NFile = 0
  KSize = 1
  MSize = KSize * 1024
  GSize = MSize * 1024
}
function Trim( InStr )
{
  gsub ( /^[\t\n\r ]*/, "", InStr )
  gsub ( /[\t\n\r ]*$/, "", InStr )
  return ( InStr )
}
function BaseName( InStr,      N, FooAry )
{
  N = split( InStr, FooAry, "/" )

  if ( N < 2 )
      return( InStr )
 
  return( FooAry [N] )
}
function Str2Regex( InStr,      OutStr, i, C )
{
  OutStr = ""

  for ( i = 1 ; i <= length( InStr ) ; i ++ )
  {
      C = substr( InStr, i, 1 )

      if ( match( C, /[A-Za-z]/ ))
        OutStr = OutStr "[" toupper( C ) tolower( C ) "]"
      else
        OutStr = OutStr C
  }
  return( OutStr )
}
function FixSize( InSize )
{
  if ( match( InSize, /G *$/ ))
  {
      return( InSize * GSize ) ;
  }
  if ( match( InSize, /M *$/ ))
  {
      return( InSize * MSize )
  }
  return( InSize +0 )
}
# main
{
  if ( DoPkgLst > 0 )
  {
      GotPkg = 0

      for ( Pkg in PkgAry )
      {
        if ( match( BaseName( FILENAME ), Pkg ))
        {
            GotPkg = 1
            break
        }
      }
      if ( GotPkg == 0 )
      {
        nextfile
      }
  }
  if ( match( $0, SizeREx ))
  {
      printf( " %8dK %s\n", FixSize( $2 ), BaseName( FILENAME ))
      NFile ++
      nextfile
  }
}
END {

  RetCode = ( NFile > 0 ) ? 0 : 1

  exit( RetCode )

}' $PkgDir/* |
LC_ALL=C sort $SortArg |
if [ "$DoTotal" != "1" ]
then
  cat
else
  gawk '
  BEGIN {
 
      T = N = 0
  }
  {
      print
      T += $1
      N ++
  }
  END {
 
      Equals = "==================================="
      printf( "# %8.8s %s\n", Equals, Equals )
      printf( "#%8dK Total Bytes in %d Packages\n", T, N )
  }'
fi
RetCode=${PIPESTATUS[0]}
exit $RetCode

Copy the code into a file in your PATH, say $HOME/bin/get-pkgsize, then
Code:

chmod 755 $HOME/bin/get-pkgsize
Thanks for the idea !

-- kjh

audriusk 04-03-2011 09:43 AM

@allend: yep, I'm getting similar results. It appears that grep's regexp engine is faster than gawk's. Perhaps because the later supports backtracking? Although this article suggests that GNU awk uses the same approach as grep. Maybe gawk is slower in general...

gnashley 04-03-2011 12:07 PM

fgrep UNCOMPRESSED /var/log/packages/* .... should be even faster

kjhambrick 04-03-2011 03:05 PM

Interesting ...

I wonder why the complex get-pkgsize script is faster than the simple awk one-liner ?

-- kjh

Code:

[konrad@kjhlt5 ~]$ time fgrep UNCOMPRESSED /var/log/packages/* | awk -F: '{print $3,$1}' | LC_ALL=C sort -rh > /dev/null

real    0m0.024s
user    0m0.014s
sys    0m0.019s

[konrad@kjhlt5 ~]$ time grep UNCOMPRESSED /var/log/packages/* | awk -F: '{print $3,$1}' | LC_ALL=C sort -rh > /dev/null

real    0m0.023s
user    0m0.017s
sys    0m0.014s

[konrad@kjhlt5 ~]$ time awk -F: '/UNCOMPRESSED/ {print $2,FILENAME}' /var/log/packages/* | LC_ALL=C sort -rh > /dev/null

real    0m0.136s
user    0m0.124s
sys    0m0.018s

[konrad@kjhlt5 ~]$ time gawk -F: '/UNCOMPRESSED/ {print $2,FILENAME}' /var/log/packages/* | LC_ALL=C sort -rh > /dev/null

real    0m0.134s
user    0m0.125s
sys    0m0.015s

[konrad@kjhlt5 ~]$ time get-pkgsize  > /dev/null

real    0m0.039s
user    0m0.030s
sys    0m0.014s



All times are GMT -5. The time now is 10:47 PM.