Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active April 22, 2024 07:57
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fernandoaleman/4dc2e514f612bc376d0f54cc3d15b608 to your computer and use it in GitHub Desktop.
Save fernandoaleman/4dc2e514f612bc376d0f54cc3d15b608 to your computer and use it in GitHub Desktop.
How to clean up Ubuntu

How To Clean Up Ubuntu

Some simple ways to free up space on Ubuntu

Remove Unnecessary Packages and Dependencies

apt-get autoremove

Clean Apt Cache

Check the amount of apt-cache on your system

du -sh /var/cache/apt

Remove all apt-cache from your system

apt-get clean

Uninstall Pakcages You Never Use

Remove specific packages

apt-get remove package-1 package-2

Remove packages and dependencies that are no longer required

apt-get autoremove

Remove Old Kernels

List your current version of linux kernel

umame -sr

Check how many linux kernels versions are installed

dpkg -l | grep linux-image | awk '{print$2}' | wc -l

Check which linux kernels versions are installed

dpkg -l | grep linux-image | awk '{print$2}'

Find the linux image number and set the LINUXIMAGE, for example linux-image-3.13.0

LINUXIMAGE=linux-image-3.13.0

Find the linux image extra number and set the LINUXIMAGEEXTRA, for example linux-image-extra-3.13.0

LINUXIMAGEEXTRA=linux-image-extra-3.13.0

Find the lowest version number and set the STARTNUM, for example 50

STARTNUM=50

Find the version number before the current version and set the ENDNUM, for example 100

ENDNUM=100

Automagically delete all the old kernel versions

for i in $(seq $STARTNUM $ENDNUM); do apt remove --purge $LINUXIMAGE-$i-generic -y; done
for i in $(seq $STARTNUM $ENDNUM); do apt remove --purge $LINUXIMAGEEXTRA-$i-generic -y; done
@lowdown2infinity
Copy link

Greetings.
There is a typo in the section "Remove Old Kernels". The first command should read uname -sr.

@haddagart
Copy link

The current kernel version that I am using is: Linux 6.5.0-26-generic

I have 21 kernel versions installed, Here's a list of them:

  • linux-image-5.15.0-100-generic
  • linux-image-5.15.0-101-generic
  • linux-image-5.15.0-91-generic
  • linux-image-5.15.0-92-generic
  • linux-image-5.15.0-94-generic
  • linux-image-5.15.0-97-generic
  • linux-image-6.2.0-26-generic
  • linux-image-6.2.0-34-generic
  • linux-image-6.2.0-35-generic
  • linux-image-6.2.0-36-generic
  • linux-image-6.2.0-37-generic
  • linux-image-6.2.0-39-generic
  • linux-image-6.5.0-14-generic
  • linux-image-6.5.0-15-generic
  • linux-image-6.5.0-17-generic
  • linux-image-6.5.0-18-generic
  • linux-image-6.5.0-21-generic
  • linux-image-6.5.0-25-generic
  • linux-image-6.5.0-26-generic
  • linux-image-generic
  • linux-image-generic-hwe-22.04

Please can you help me decide on the rest of the commands above. I am not that profecient with linux kernels

@lowdown2infinity
Copy link

Please can you help me decide on the rest of the commands above. I am not that profecient with linux kernels

Hello. Have you tried: sudo apt-get autoremove --purge

This is usually enough.

@haddagart
Copy link

Please can you help me decide on the rest of the commands above. I am not that profecient with linux kernels

Hello. Have you tried: sudo apt-get autoremove --purge

This is usually enough.

Yes I regularly do this and everything is fine.

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Except that I a bit uncomfortable with the number of kernel images that I have. But I don't want to risk my os stability before consulting with the community about the right way to do it for my specific case.

@lowdown2infinity
Copy link

  • linux-image-generic
  • linux-image-generic-hwe-22.04

Leave those last two alone and you can safely get rid of all the others.

@haddagart
Copy link

  • linux-image-generic
  • linux-image-generic-hwe-22.04

Leave those last two alone and you can safely get rid of all the others.

Thank you so much for your feedback

@emanuti
Copy link

emanuti commented Apr 8, 2024

Thanks for share! Repkace umame -sr by uname -sr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment