Cleaning up an Ubuntu server

Recently I have run into an HDD space problem with one of my Ubuntu server systems. It’s been up for a few years, without special maintenance from this point of view. So, here are the steps I have taken:

Cleanup the system:

  • sudo apt-get clean
  • sudo apt-get autoclean
  • sudo apt-get autoremove


Cleanup the logfiles:

  • shopt -s globstar # if needed
  • truncate -s 0 /var/log/*.log # first-level logs
  • truncate -s 0 /var/log/**/*.log # nested folders, like /var/log/nginx/access.log
  • find /var/log -type f -name ‘*.[0-99].gz’ -exec rm {} +
  • Extra:
    find /var/log -type f -name ‘*.log.[0-99].gz’ -exec rm {} +


Clean the btmp file:

  • cat /dev/null > /var/log/btmp


How To Clear btmp File

You can manually delete files that you do not need or which have not been cleaned by the commands above. Anyway, please be carefull and as a recommendation: do not delete folders inside /var/logs.