[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Automatic removing of unneeded .deb files?



* Matej Cepl <ceplm@seznam.cz> [051123 12:25]:
> Eric Cooper wrote:
> >     #!/bin/bash
> > 
> >     for deb in /var/cache/apt/archives/*.deb; do
> > pkg=$(dpkg-deb -f $deb Package)
> > if ! dpkg-query -Wf '${Status}' $pkg | grep -q ' installed'; then
> > echo $pkg: not installed
> > # uncomment this to remove it ...
> > # rm $deb
> > fi
> 
> Thanks. There is now wish for this in BTS (bug# 340444).
> 
> Matej
> 

Matej's python script (in the above bug) is very nice.

I was disappointed with the performance of the above shell script,
though, so before I saw that Matej had uploaded his python script, I
reworked the shell script to only call dpkg-query once for all files
rather than once for each file.  It is useful for informational
purposes, even if you have Matej's script (especially if you are more
comfortable editing bash than python).  Note the assumptions for the
line that actually removes the .deb files; I would not use my script to
remove the .debs until I had used it to show not-installed packages.
(Which ATM requires commenting and uncommenting lines; if I start to use
this script frequently, I'll clean it up to use arguments.)

Matej:  Can you modify your script to only remove not-installed .debs,
leaving .debs for removed-but-not-purged and not-configured packages
alone?  Accept my thanks in advance if you feel like doing it.

Also, I'm not fluent in python, but it looks like the line
    if not(item[0][0] in currNames) and not(item[0][1] in currVersions):
allows item[0][1] to match a version in currVersions for a different
package.  Is that correct?  Also, the logic should probably be
not(a and b) rather than not(a) and not(b), where b is item[0][1] ==
corresponding-element-of-currVersions.

I also noticed that the aptitude search truncated the version number,
which could have detrimental effects.

...Marvin

#!/bin/bash

pkgs=""
for deb in /var/cache/apt/archives/*.deb
do
	pkgs="$pkgs $(dpkg-deb -f $deb Package)"
done

# Uncomment one of the lines below

# This shows only not-installed packages
#dpkg-query --show --showformat='${Package} ${Status}\n' $pkgs | fgrep not-installed

# This shows packages that are not fully installed (e.g. removed but not purged)
dpkg-query --show --showformat='${Package} ${Status}\n' $pkgs | fgrep -v ' installed'

# This just echoes the package names without the status
#dpkg-query --show --showformat='${Package} ${Status}\n' $pkgs | fgrep not-installed | awk -- '{print $1}'

# This removes the .deb files that are not-installed
# This relies on the .deb file being named pkg_version_arch.deb; it also
#   does not consider packages with .deb's for more than one version
#rm `dpkg-query --show --showformat='${Package} ${Status}\n' $pkgs | fgrep not-installed | awk -- '{printf "/var/cache/apt/archives/%s_*.deb ", $1}'`


Reply to: