-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclean-system.sh
70 lines (62 loc) · 2.08 KB
/
clean-system.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# @title: Clean System
# @description: Removing unused apt packages, kernels, thumbnail cache, and docker objects.
# Tested on Ubuntu Server 20.04 LTS and Raspberry Pi OS
# Variables for pretty printing
RED=$(tput bold)$(tput setaf 1) # Red Color
GREEN=$(tput bold)$(tput setaf 2) # Green Color
NC=$(tput sgr0) # No Color
BEGIN=$(df /home --output=used | grep -Eo '[0-9]+')
# Checking root/sudo permissions
if [ "$(id -u)" -ne "0" ]; then
echo "${RED}Please run as root${NC}"
exit
fi
# Removing unused packages and cache (APT)
echo -e "${RED}Cleaning Unused Packages...${NC}" &&
sudo apt-get -y autoremove --purge &&
sudo apt-get clean &&
# Removing Old Unused Linux Kernels
IN_USE=$(uname -a | awk '{ print $3 }')
echo -e "${GREEN}Your in use kernel is ${IN_USE} ${NC}"
OLD_KERNELS=$(
dpkg --list |
grep -v "$IN_USE" |
grep -Ei 'linux-image|linux-headers|linux-modules' |
awk '{ print $2 }'
)
# skipcq: SH-2154
if [ "${#files[@]}" -ne "0" ]; then
echo -e "\n${GREEN}Old Kernels to be removed:${NC}"
echo -e "${GREEN}$OLD_KERNELS${NC}\n"
read -r -p "${RED}Do you want to delete the old kernels? [y/N]${NC} " response
case "$response" in
[yY][eE][sS] | [yY])
for PACKAGE in $OLD_KERNELS; do
yes | apt purge "$PACKAGE"
done
;;
*)
echo -e "${RED}Skipping Removing old kernel...${NC}"
;;
esac
else
echo -e "${GREEN}No old unused kernel to clean.${NC}"
fi
# Cleaning Thumbnail Cache
echo -e "${RED}Cleaning Thumbnails...${NC}" &&
sudo rm -rf ~/.cache/thumbnails/* &&
# Pruning Docker Objects
echo -e "${RED}Pruning Docker images, volumes, and networks...${NC}" &&
docker image prune -a -f --filter "until=24h" &&
docker volume prune -f &&
docker network prune -f
# Delete journal logs older than 5 days
sudo journalctl --vacuum-time=5days
# Summarization
END=$(df /home --output=used | grep -Eo '[0-9]+')
RECLAIMED=$((BEGIN - END))
if [ $RECLAIMED -lt 0 ]; then
RECLAIMED=0
fi
echo "${GREEN}${RECLAIMED} KB Reclaimed. ${NC}"