Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker
Forked from Iman/clean.sh
Last active February 18, 2022 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SerhatTeker/52274925dd75c3ea03a0fd4a04f47e9f to your computer and use it in GitHub Desktop.
Save SerhatTeker/52274925dd75c3ea03a0fd4a04f47e9f to your computer and use it in GitHub Desktop.
Free up disk space on Ubuntu - clean log, cache, archive packages/apt archives, orphaned packages, old kernel and remove the trash
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# vim: set ft=sh et ts=4 sw=4 sts=4:
# INFO
# --------------------------------------------------------------------------------------
# Free up disk space on Debian, Ubuntu - clean log, cache, archive packages/apt archives,
# orphaned packages, old kernel and remove the trash
#
# Gist repo:
# https://gist.github.com/SerhatTeker/1a6c6c49b58ed5531156bece4c666994
# Upstream fork:
# https://gist.github.com/Iman/8c4605b2b3ce8226b08a
# USAGE
# --------------------------------------------------------------------------------------
# $ curl -fsSL https://gist.githubusercontent.com/SerhatTeker/52274925dd75c3ea03a0fd4a04f47e9f/raw/clean-ubuntu | sudo bash
# or
# $ wget -O - https://gist.githubusercontent.com/SerhatTeker/52274925dd75c3ea03a0fd4a04f47e9f/raw/clean-ubuntu | sudo bash
# Bash safeties: exit on error, no unset variables, pipelines can't hide errors
set -o errexit
set -o nounset
set -o pipefail
_is_linux() {
local uname="$(uname -a)"
if [[ ! "$uname" =~ Linux ]]; then
echo "Not Linux OS!"
exit
fi
}
# Ensure running as root
check_is_sudo() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root!"
exit
fi
}
# Check the Drive Space Used by Cached Files
_apt_archive() {
sudo du -sh /var/cache/apt/archives
}
# Clean all the log file
# for logs in `find /var/log -type f`; do > $logs; done
_logs() {
logs=`sudo find /var/log -type f`
for i in $logs
do
> $i
done
}
# Getting rid of partial packages
_partial() {
sudo apt-get clean && sudo apt-get autoclean
}
# Commented out
# To avoid harming your system by removing key packages
_software_common() {
# apt-get remove --purge -y software-properties-common
:
}
# Getting rid of no longer required packages
_auto_remove() {
sudo apt-get autoremove -y
}
# Getting rid of orphaned packages
_orphan() {
deborphan | xargs sudo apt-get -y remove --purge
}
# Free up space by clean out the cached packages
_cached_packages() {
sudo apt-get clean
}
# Remove the Trash
_thrash() {
sudo rm -rf /home/*/.local/share/Trash/*/**
sudo rm -rf /root/.local/share/Trash/*/**
}
# Remove Man
_man() {
sudo rm -rf /usr/share/man/??
sudo rm -rf /usr/share/man/??_*
}
# Delete all .gz and rotated file
_rotated() {
sudo find /var/log -type f -regex ".*\.gz$" | xargs rm -Rf
sudo find /var/log -type f -regex ".*\.[0-9]$" | xargs rm -Rf
}
# Cleaning the old kernels
_kernels() {
sudo dpkg-query -l | grep linux-im*
# dpkg-query -l |grep linux-im*|awk '{print $2}'
sudo apt-get purge \
$(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | head -n -1) \
--assume-yes
sudo apt-get install linux-headers-`uname -r|cut -d'-' -f3`-`uname -r|cut -d'-' -f4`
}
main() {
_is_linux
check_is_sudo
echo "Cleaning is started"
_apt_archive
_logs
_partial
_software_common
_auto_remove
_orphan
_cached_packages
_thrash
_man
_rotated
_kernels
echo "Cleaning is completed"
}
main
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment