Topic: How to update&clean the system

Hi everybody!

I usually run this script for updating and cleaning the system..
What do you think about? Would you add or remove something?

Here the code..

#!/bin/bash

apt-get update
apt-get dist-upgrade -y
apt-get upgrade -y
apt-get install -f
apt-get autoclean
apt-get autoremove -y
apt-get clean


dpkg --purge `COLUMNS=300 dpkg -l "*" | egrep "^rc" | cut -d" " -f3`

echo -n "# Remove old kernels "
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get --dry-run remove
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y remove

Thx in advance for any suggestion!

Re: How to update&clean the system

Hi smile and welcome to CB Land

I am installing/upgrading today - will be testing

Re: How to update&clean the system

Hi Vansgo!
Thx for the welcome! big_smile

Let me know about the script's testing! wink

Re: How to update&clean the system

I ran some of these commands with the "cb-welcome" script

installing some jack audio updates and then running autoclean smile

Re: How to update&clean the system

I ran this and get the following errors at the start

david@crunchbang:~$ update_and_clean 
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
E: Could not open lock file /var/cache/apt/archives/lock - open (13: Permission denied)
E: Unable to lock the download directory
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
E: Could not open lock file /var/cache/apt/archives/lock - open (13: Permission denied)
E: Unable to lock the download directory
dpkg: requested operation requires superuser privilege
# Remove old kernels [sudo] password for david: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-2.6-amd64 linux-headers-2.6-liquorix-amd64
  linux-headers-2.6.32-5-amd64 linux-headers-2.6.32-5-common
  linux-headers-2.6.36-2.dmz.5-liquorix-amd64 linux-image-2.6-amd64
  linux-image-2.6-liquorix-amd64 linux-image-2.6.32-5-amd64
  linux-image-2.6.36-2.dmz.5-liquorix-amd64 linux-kbuild-2.6.32
0 upgraded, 0 newly installed, 10 to remove and 107 not upgraded.
Remv linux-headers-2.6-amd64 [2.6.32+29]
Remv linux-headers-2.6-liquorix-amd64 [2.6.36-25]
Remv linux-headers-2.6.32-5-amd64 [2.6.32-41]
Remv linux-headers-2.6.32-5-common [2.6.32-41]
Remv linux-headers-2.6.36-2.dmz.5-liquorix-amd64 [2.6.36-25]
Remv linux-image-2.6-amd64 [2.6.39+35.1~bpo60+1]
Remv linux-image-2.6-liquorix-amd64 [2.6.36-25]
Remv linux-image-2.6.32-5-amd64 [2.6.32-38]
Remv linux-image-2.6.36-2.dmz.5-liquorix-amd64 [2.6.36-25]
Remv linux-kbuild-2.6.32 [2.6.32-1]
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-2.6-amd64 linux-headers-2.6-liquorix-amd64 linux-headers-2.6.32-5-amd64 linux-headers-2.6.32-5-common linux-headers-2.6.36-2.dmz.5-liquorix-amd64 linux-image-2.6-amd64 linux-image-2.6-liquorix-amd64
  linux-image-2.6.32-5-amd64 linux-image-2.6.36-2.dmz.5-liquorix-amd64 linux-kbuild-2.6.32
0 upgraded, 0 newly installed, 10 to remove and 107 not upgraded.
After this operation, 294 MB disk space will be freed.
(Reading database ... 131165 files and directories currently installed.)
Removing linux-headers-2.6-amd64 ...
Removing linux-headers-2.6-liquorix-amd64 ...
Removing linux-headers-2.6.32-5-amd64 ...
Removing linux-headers-2.6.32-5-common ...
Removing linux-headers-2.6.36-2.dmz.5-liquorix-amd64 ...
Removing linux-image-2.6-amd64 ...
Removing linux-image-2.6-liquorix-amd64 ...
Removing linux-image-2.6.32-5-amd64 ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms 2.6.32-5-amd64 /boot/vmlinuz-2.6.32-5-amd64
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 2.6.32-5-amd64 /boot/vmlinuz-2.6.32-5-amd64
update-initramfs: Deleting /boot/initrd.img-2.6.32-5-amd64
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 2.6.32-5-amd64 /boot/vmlinuz-2.6.32-5-amd64
Generating grub.cfg ...
Found background image: desktop-grub.png
Found linux image: /boot/vmlinuz-2.6.39-bpo.2-amd64
Found initrd image: /boot/initrd.img-2.6.39-bpo.2-amd64
Found linux image: /boot/vmlinuz-2.6.36-2.dmz.5-liquorix-amd64
Found initrd image: /boot/initrd.img-2.6.36-2.dmz.5-liquorix-amd64
Found Windows 7 (loader) on /dev/sda1
done
Removing linux-image-2.6.36-2.dmz.5-liquorix-amd64 ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms 2.6.36-2.dmz.5-liquorix-amd64 /boot/vmlinuz-2.6.36-2.dmz.5-liquorix-amd64
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 2.6.36-2.dmz.5-liquorix-amd64 /boot/vmlinuz-2.6.36-2.dmz.5-liquorix-amd64
update-initramfs: Deleting /boot/initrd.img-2.6.36-2.dmz.5-liquorix-amd64
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 2.6.36-2.dmz.5-liquorix-amd64 /boot/vmlinuz-2.6.36-2.dmz.5-liquorix-amd64
Generating grub.cfg ...
Found background image: desktop-grub.png
Found linux image: /boot/vmlinuz-2.6.39-bpo.2-amd64
Found initrd image: /boot/initrd.img-2.6.39-bpo.2-amd64
Found Windows 7 (loader) on /dev/sda1
done
The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old 
 you may need to re-run your boot loader
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old 
 you may need to re-run your boot loader
Removing linux-kbuild-2.6.32 ...

Do I need to change something in the script or run it as

david@crunchbang:~$ sudo update_and_clean 
FB | G+ | T

Re: How to update&clean the system

^ you probably need sudo, yeah. whenever i see 'permission denied', i know it must be a rights-issue, and 9 out of 10 times i just need to use sudo.

Re: How to update&clean the system

Workes for me,thanks a million...and hello!

Re: How to update&clean the system

I saved the script at:

 ~/bin/update_and_clean

but when i try to run the script from the terminal with the the following command:

sudo update_and_clean

all i get is the following message:

sudo: update_and_clean: command not found

Any idea why?

#! Statler (10-20120207) 32bit-BPO Openbox

Re: How to update&clean the system

Is ~/bin in your PATH? wink

Re: How to update&clean the system

^ ~/bin or /home/kris/bin is in my path.  I can execute another script from rhowaldt OK, it appears to be this one only. sad

#! Statler (10-20120207) 32bit-BPO Openbox

Re: How to update&clean the system

Is the script executable? Try this:

cd ~/bin
chmod +x update_and_clean
sudo ./update_and_clean

If that works then try running it from somewhere else.

Re: How to update&clean the system

^ That worked, it's going through the process now, hopefully my PC will still be working when it's finished. lol  I shall try running from else where after it's finished.

#! Statler (10-20120207) 32bit-BPO Openbox

Re: How to update&clean the system

In general, it's not considered a good idea to write a script that has to be run with as a sudoer; it's better to prepend sudo to each line within the script (`sudo apt-get update` etc.) that needs sudoer privileges.

When you first attempted to run the script with `sudo update_and_clean`, were you by any chance *in /bin*?  For some reason, the PATH seems to have a blind spot to whatever your current directory is, even if it's explicitly declared as part of your PATH.  That could explain why the above command wouldn't work, while `sudo ./update_and_clean` did.

while ( ! ( succeed = try() ) );

Re: How to update&clean the system

^ The script was located in ~/bin if that's what you mean.  When I tried to execute it and it failed via the terminal I was not at ~/bin

#! Statler (10-20120207) 32bit-BPO Openbox

Re: How to update&clean the system

kri5 wrote:

When I tried to execute it and it failed via the terminal I was not at ~/bin

This is what I meant.  OK, so the problem was just the executable flag.

I'd still put the sudos within the script and run it with just `update_and_clean`; when the script hits the first sudo, it will ask for your sudoer passwd before continuing.

while ( ! ( succeed = try() ) );

Re: How to update&clean the system

@pvsage.. You are indeed a genius, adding 'sudo' to the lines in the script instead of trying to execute the script with 'sudo' did the trick, it now appears to work fine.  big_smile

#! Statler (10-20120207) 32bit-BPO Openbox