You are not logged in.
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
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
@danielsoft,
Thanks - that's very useful and it is a nice gentle introduction to Python for me!
Offline
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
an even cleaner way would be to use string formatting
return "%d %s" % (self.size, self.name )
Offline
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!
Offline
#!/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
Allan updated his https://aur.archlinux.org/packages.php?ID=43779
Offline
Not going to sell expac for me, karol?
expac '%m\t%n' | sort -h
Mine is better
Offline
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
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
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
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