You are not logged in.

#1 2008-03-09 17:53:59

danielsoft
Member
From: Czech Republic
Registered: 2008-02-16
Posts: 102

pacsize: list of packages sorted by disk space usage

Hello,
I was wondering, which packages on my HDD are the largest, so I created this little script in Python
(another motivation was to learn Python more...)

it will just output a list of your packages,  sorted by size, starting from the largest, to the standard output
the output is in format

272495 openoffice-base
138817 tetex
115541 acroread
102316 jre
95587 jdk
65836 kernel26
65649 wine
...

for example

here is the script:

#!/usr/bin/env python
import re
import os

class Package:
    def __init__(self,name,size):
        self.name = name
        self.size = int(round(float(size)))
    def __repr__(self):
        return `self.size`+" "+self.name    
    def __cmp__(self,other):
        return cmp(other.size,self.size) #desc

paclist = []        

re_name = re.compile(r'Name\s*:\s*(\S+)')
re_size = re.compile(r'Installed Size\s*:\s*([0-9.]+).*')

p = os.popen("pacman -Qi","r")

for line in p.readlines():
    match_name = re_name.match(line)
    match_size = re_size.match(line)
    if match_name : 
            package_name = match_name.groups(1)[0]
    if match_size :
            package_size = match_size.groups(1)[0]
            paclist.append(Package(package_name,package_size))

p.close()

paclist.sort()
for pac in paclist: print pac

I called it "pacsize".

alternatively you can download it here


may the Source be with you

Offline

#2 2008-03-09 18:25:46

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: pacsize: list of packages sorted by disk space usage

There were a thread with several ways to achieve that result :
http://bbs.archlinux.org/viewtopic.php?id=36529

I posted an update of my libalpm example there :
http://bbs.archlinux.org/viewtopic.php?id=42707

Last edited by shining (2008-03-09 18:26:57)


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2008-03-09 18:38:06

ninian
Member
From: United Kingdom
Registered: 2008-02-24
Posts: 726
Website

Re: pacsize: list of packages sorted by disk space usage

@danielsoft,

Thanks - that's very useful and it is a nice gentle introduction to Python for me! smile

Offline

#4 2008-03-30 17:16:42

sabooky
Member
Registered: 2006-11-02
Posts: 89

Re: pacsize: list of packages sorted by disk space usage

awk is awesome for small tasks like this.

pacman -Qi|awk '/^Installed Size/{print int($4), name} /^Name/{name=$3}'|sort -nr

That being said, great job of using python in an OO manner.
One comment on your python code.

return `self.size`+" "+self.name

back quotes are deprecated and will be removed in p3k. p3k changes use repr() instead of that's what you intended.

Here's an example of back quotes, repr(), and str()

>>> import datetime
>>> a=datetime.datetime.now()
>>> repr(a)
'datetime.datetime(2008, 3, 30, 13, 12, 29, 664490)'
>>> `a`
'datetime.datetime(2008, 3, 30, 13, 12, 29, 664490)'
>>> str(a)
'2008-03-30 13:12:29.664490'
>>> print `a`
datetime.datetime(2008, 3, 30, 13, 12, 29, 664490)
>>> print a
2008-03-30 13:12:29.664490

Offline

#5 2008-03-30 17:47:00

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: pacsize: list of packages sorted by disk space usage

an even cleaner way would be to use string formatting

return "%d %s" % (self.size, self.name )

Offline

#6 2008-12-14 18:45:00

muczyjoe
Member
From: Szeged (Hungary)
Registered: 2007-05-16
Posts: 45
Website

Re: pacsize: list of packages sorted by disk space usage

sabooky: I made some modifications:

LC_ALL="C" /usr/bin/pacman -Qi | awk '/^Installed Size/{print int($4), name} /^Name/{name=$3}' | sort -nr

LC_ALL needs to be set, because, with other languages, this produces an empty output.
The absolute path also needs to be set, because users of pacman-color.
Btw. thanks for the script! smile

Offline

#7 2011-07-19 07:28:13

Nisstyre56
Member
From: Canada
Registered: 2010-03-25
Posts: 85

Re: pacsize: list of packages sorted by disk space usage

#!/usr/bin/env python2

import subprocess
from itertools import izip

p = subprocess.check_output(["pacman", "-Qi"])
p = p.split("Name")

def sizes():
    for i,x in enumerate(p):
        try:
            yield p[i].split("Installed Size : ")[1].split("\n")[0]
        except Exception:
            pass
def names():
    for i,x in enumerate(p):
        try:
            yield p[i].split(":")[1].split("\n")[0].strip()
        except Exception:
            pass

final_list = sorted(((size,name) for size, name in izip(sizes(), names())), key=lambda x: int(x[0].split(".")[0]))
for size, name in final_list:
    print "%s has a size of %s" % (name, size)

Another way to do it. Someone was asking for this on IRC so I hacked this together in 10 minutes. It abuses the try, except construct heavily, so please don't take this as being good python. This should be much faster than the original one though.

real    0m0.209s
user    0m0.147s
sys    0m0.030s

Also, the reason I'm bumping this thread is because the older ones might not work anymore, and someone else might benefit from this.

Edit: changed zip to izip, should improve speed for anyone who wants to use it

Last edited by Nisstyre56 (2011-07-19 21:08:19)


In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage

Offline

#8 2011-07-19 09:52:55

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: pacsize: list of packages sorted by disk space usage

Offline

#9 2011-07-19 10:00:22

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,110
Website

Re: pacsize: list of packages sorted by disk space usage

Not going to sell expac for me, karol?

expac '%m\t%n' | sort -h

Offline

#10 2011-07-19 10:02:37

Allan
Member
From: Brisbane, AU
Registered: 2007-06-09
Posts: 10,902
Website

Re: pacsize: list of packages sorted by disk space usage

falconindy wrote:

Not going to sell expac for me, karol?

expac '%m\t%n' | sort -h

Mine is better tongue

Offline

#11 2011-07-19 10:02:59

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: pacsize: list of packages sorted by disk space usage

falconindy wrote:

Not going to sell expac for me, karol?

expac '%m\t%n' | sort -h

This is what happens when I post before my first coffee.
I think Allan's script counts size differently.

Last edited by karol (2011-07-19 10:06:17)

Offline

#12 2011-07-19 10:05:54

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,110
Website

Re: pacsize: list of packages sorted by disk space usage

Ahh yes, Allan's pulls in dependencies, which is a noble goal.

Offline

#13 2011-07-19 20:50:43

Nisstyre56
Member
From: Canada
Registered: 2010-03-25
Posts: 85

Re: pacsize: list of packages sorted by disk space usage

falconindy wrote:

Not going to sell expac for me, karol?

expac '%m\t%n' | sort -h

Okay expac is ridiculously faster. Everyone use that.


In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage

Offline

#14 2019-03-27 20:58:27

eatkin
Member
Registered: 2019-03-27
Posts: 1

Re: pacsize: list of packages sorted by disk space usage

Came here looking for answers and was intrigued by sabooky's awk solution but it didn't take in to account the variable units (perhaps the output format has changed since 2008!). I spent a little time learning awk and came up with this:

cat > pacman_sizes.sh << EOF
#!/bin/sh
pacman -Qi | awk $@ '
    BEGIN {
        units["B"] = 0
        units["KiB"] = 1
        units["MiB"] = 2
        units["GiB"] = 3
        if (unit == "") unit = "MiB"
        if (min == "") min = 50
        if (pad == "") pad = 10
    }
    /^Name/ {name=$3}
    /^Installed Size/ {
        size = (int($4) * 1024^units[$5]) / 1024^units[unit]
        if (size > min) printf "% *.1f %s %s\n", pad, size, unit, name
    }
' | sort -n
EOF

chmod a+x pacman_sizes.sh
./pacman_sizes.sh -v unit=KiB -v min=100

Offline

#15 2019-03-27 21:16:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,267
Website

Re: pacsize: list of packages sorted by disk space usage

The units aren't actually in the local database, they are just added by pacman, so you are having pacman add them, then you are taking them away again: cut out the middle man:

sed -n '/%NAME%/{n;h;}; /%SIZE%/ {n;H;x;s/\n/\t/;p;}' /var/lib/pacman/local/*/desc | sort -rnk2

But this is ancient anyways.

Last edited by Trilby (2019-03-27 21:18:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB