"apt-get dist-upgrade" in limited hard drive space

Having a small hard drive can make dist-upgrades a complete nightmare, as apt-get likes to download everything and then dist-upgrade, filling up your harddrive before the download is complete.

However, there is a way round it. This is how I did it. First, create a file which lists the packages that will be updated thus:

apt-get -s -u upgrade || sed -n -e ''^Inst'',/^Conf/p -e /^Conf/q || \
     cut -d" " -f2 > update.packages

This will create a file called update.packages which has a list of packages to update. Before you do anything else, have a look at it and guage whether you think it is too long. Any packages that you recognise to be heavyweights, take them out for now. If you're running really low end, you can even update it a package at a time.

Next, you need to download those packages:

for f in $(cat update.packages); do apt-get install $f; done

This runs a loop which looks at the packages in update.packages and installs them.

Next, you need to clean things up to make room for the next cycle of install with:

apt-get clean

Then repeat the process until update.packages no longer has any text in it (which means there are no other packages to update).

Finally, you can carry out apt-get dist-upgrade, which on my machine managed to work. This could still fill up your machine in theory.

I'm interested to hear if anyone can script this, so that it becomes more automatic. Please leave any suggestions here.