lyncd

One-liner to clean DNF package cache but keep latest version of each package

If you’re running Fedora or Red Hat and have turned on the DNF package cache, eventually you’ll start using gigabytes of disk space. Simply running dnf clean packages will erase everything in the package cache.

But what if you want to preserve the latest version of each package and just delete the outdated versions? For example, to keep a local cache of the latest version of each package you’ve installed, and so that DRPM will work.

Here’s how:

cd /var/cache/dnf
for y in `find . -maxdepth 2 -type d -name 'packages'`; do cd $y; for x in `ls *.rpm | grep -Po '^.*?(?=-[0-9])' | sort -r | uniq`; do ls ${x}-*.rpm | grep -P "^${x}-[0-9]" | head -n -1 | xargs rm -f; done; cd ../..; done

Disk space used before and after on my laptop:

cd /var/cache/dnf
du -hs .
7.5G	.
# run one-liner above
du -hs .
1.8G	.

Filed under: Blog, Code, Systems.  Tagged: , .

No comments »

No comments yet.

Add a comment

You can also log in (or register) for easier commenting on lyncd.

Lyncd is an evolving group blog-in-progress. Weʼll be designing the site, adding users and content, and developing a hifalutin site focus as we go. For now, just think of our site mantra as “minimalism.” :)