tcs-cleaner/tcs-cleaner

388 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/bash
version="v0.4.6"
if [ ! -z $1 ]
then
if [ $1 = "--su" ]
then
su root -c "tcs-cleaner $USER"
exit
fi
if [ $1 = "--sudo" ]
then
sudo tcs-cleaner $USER
exit
fi
if [ $1 = "--pkexec" ]
then
pkexec tcs-cleaner $USER
exit
fi
if [ $1 = "-v" ] || [ $1 = "--version" ]
then
echo $version
exit
fi
if [ $1 = "-h" ] || [ $1 = "--help" ]
then
echo "tcs-cleaner: clean
Auto detect:
--pkexec start with current user with pkexec
--sudo start with current user with sudo
--su start with current user with su root
Use as root:
tcs-cleaner <user>
commands:
-h or --help help
-v or --version see version"
exit
fi
if [[ ! $(getent passwd $1) = "" ]]
then
user=$1
else
echo "$1 not exists"
exit
fi
else
echo "your user?"
read user
if [ ! $(getent passwd $user) = "" ]
then
echo "good"
else
echo "$user not exists"
exit
fi
fi
clear
if [ ! $(whoami) = "root" ]
then
if [ -f "/usr/bin/dialog" ] || [ -f "/usr/sbin/dialog" ] || [ -f "/usr/games/dialog" ]
then
dialog --title "root" --msgbox "You need run this script as root" 0 0
else
echo "You need run this script as root"
fi
clear
exit
fi
# Detect package manager
if [ -f "/usr/bin/zypper" ] || [ -f "/usr/sbin/zypper" ] || [ -f "/usr/games/zypper" ]
then
PAQ="zypper"
if [ ! -f "/bin/dialog" ] || [ ! -f "/bin/curl" ]
then
zypper -n install dialog curl coreutils bash
fi
elif [ -f "/usr/bin/apt" ] || [ -f "/usr/sbin/apt" ] || [ -f "/usr/games/apt" ]
then
PAQ="apt"
if [ ! -f "/bin/dialog" ] || [ ! -f "/bin/curl" ]
then
yes | apt install dialog curl coreutils bash
fi
elif [ -f "/usr/bin/dnf" ] || [ -f "/usr/sbin/dnf" ] || [ -f "/usr/games/dnf" ]
then
PAQ="dnf"
if [ ! -f "/bin/dialog" ] || [ ! -f "/bin/curl" ]
then
yes | dnf -y install dialog curl coreutils bash
fi
elif [ -f "/usr/bin/pacman" ] || [ -f "/usr/sbin/pacman" ] || [ -f "/usr/games/pacman" ]
then
PAQ="pacman"
if [ ! -f "/bin/dialog" ] || [ ! -f "/bin/curl" ]
then
yes | sudo pacman -S dialog curl coreutils bash
fi
elif [ -f "/usr/bin/eopkg" ] || [ -f "/usr/sbin/eopkg" ] || [ -f "/usr/games/eopkg" ]
then
PAQ="eopkg"
if [ ! -f "/bin/dialog" ] || [ ! -f "/bin/curl" ]
then
yes | eopkg install dialog curl coreutils bash
fi
fi
clear
last_version=$(curl https://raw.githubusercontent.com/Can202/tcs-cleaner/main/version)
dialog --title "Alert" --yesno "this script is a thorough cleaning, recommended: read very well, what the script is doing. Continue?" 0 0
if [ ! $? = 0 ]
then
clear
exit
fi
dialog --title "News $version" --msgbox "Hi!
- fix pacman error
" 0 0
if [ -f "/usr/bin/tcs-cleaner" ]
then
if [ ! $last_version = $version ]
then
if [[ ! $last_version == *" "* ]]
then
dialog --title "New Update!!" --yesno "There is a new update of this script, you can install it, do you want to do?" 0 0
if [ $? = 0 ]
then
clear
curl https://raw.githubusercontent.com/Can202/tcs-cleaner/$last_version/installer | bash
tcs-cleaner $1
exit
fi
fi
fi
fi
dialog --title "Update System" --yesno "You can update your machine if you want" 0 0
if [ $? = 0 ]
then
if [ $PAQ = "zypper" ]
then
dialog --title "zypper" --yesno "Update from zypper?" 0 0
if [ $? = 0 ]
then
clear
zypper ref
zypper up
fi
fi
if [ $PAQ = "apt" ]
then
dialog --title "apt" --yesno "Update from apt?" 0 0
if [ $? = 0 ]
then
clear
apt update
apt upgrade
fi
fi
if [ $PAQ = "dnf" ]
then
dialog --title "dnf" --yesno "Update from dnf?" 0 0
if [ $? = 0 ]
then
clear
dnf upgrade --refresh
fi
fi
if [ $PAQ = "pacman" ]
then
dialog --title "pacman" --yesno "Update from pacman?" 0 0
if [ $? = 0 ]
then
clear
pacman -Syu
fi
fi
if [ $PAQ = "eopkg" ]
then
dialog --title "eopkg" --yesno "Update from eopkg?" 0 0
if [ $? = 0 ]
then
clear
eopkg upgrade
fi
fi
dialog --title "Alert" --yesno "If the update was to big, it's recommended restart, and then continue with the script. Yes = Continue, No = Exit" 0 0
if [ ! $? = 0 ]
then
clear
exit
fi
if [ -f "/usr/bin/flatpak" ] || [ -f "/usr/sbin/flatpak" ] || [ -f "/usr/games/flatpak" ]
then
dialog --title "flatpak" --yesno "Update flatpak?" 0 0
if [ $? = 0 ]
then
clear
flatpak update
fi
fi
if [ -f "/usr/bin/snap" ] || [ -f "/usr/sbin/snap" ] || [ -f "/usr/games/snap" ]
then
dialog --title "snap" --yesno "Update snap?" 0 0
if [ $? = 0 ]
then
clear
snap refresh
fi
fi
fi
dialog --title "remove cache" --yesno "You can remove cache files of a package manager, if you want" 0 0
if [ $? = 0 ]
then
if [ $PAQ = "zypper" ]
then
dialog --title "zypper" --yesno "remove unnecessary packages and clean chache from zypper?" 0 0
if [ $? = 0 ]
then
clear
zypper packages --unneeded | awk -F'|' 'NR==0 || NR==1 || NR==2 || NR==3 || NR==4 {next} {print $3}' | grep -v Name | sudo xargs zypper remove --clean-deps
zypper cc
fi
fi
if [ $PAQ = "apt" ]
then
dialog --title "apt" --yesno "remove unnecessary packages and clean chache from apt?" 0 0
if [ $? = 0 ]
then
clear
apt autoremove
apt autoclean
apt clean
fi
fi
if [ $PAQ = "dnf" ]
then
dialog --title "dnf" --yesno "remove unnecessary packages and clean chache from dnf?" 0 0
if [ $? = 0 ]
then
clear
dnf autoremove
dnf clean all
fi
if [ -f "/usr/bin/yum" ] || [ -f "/usr/sbin/yum" ] || [ -f "/usr/games/yum" ]
then
dialog --title "yum" --yesno "remove unnecessary packages and clean chache from yum?" 0 0
if [ $? = 0 ]
then
clear
yum clean all
fi
fi
fi
if [ $PAQ = "pacman" ]
then
dialog --title "pacman" --yesno "remove unnecessary packages and clean chache from pacman?" 0 0
if [ $? = 0 ]
then
clear
pacman -Rs $(pacman -Qtdq)
pacman -Sc
fi
fi
if [ $PAQ = "eopkg" ]
then
dialog --title "eopkg" --yesno "clean cache from eopkg?" 0 0
if [ $? = 0 ]
then
clear
eopkg delete-cache
fi
fi
if [ -f "/usr/bin/flatpak" ] || [ -f "/usr/sbin/flatpak" ] || [ -f "/usr/games/flatpak" ]
then
dialog --title "flatpak" --yesno "clean cache from flatpak?" 0 0
if [ $? = 0 ]
then
su $user -c 'rm -vrf ~/.cache/flatpak/* 2>/dev/null'
rm -vrf /var/tmp/flatpak-cache/*
fi
fi
if [ -f "/usr/bin/snap" ] || [ -f "/usr/sbin/snap" ] || [ -f "/usr/games/snap" ]
then
dialog --title "snap" --yesno "clean cache from snap?" 0 0
if [ $? = 0 ]
then
rm -vrf /var/lib/snapd/cache/*
fi
fi
dialog --title "user cache" --yesno "Clean cache folders:
- thumbnails user cache folder
- pip, firefox and flatpak user chache
- fontconfig and mesa_shader_cache cache
- all cache files, with 10 day without access
- delete some cache files on wine
- all flatpak and snap apps cache files, with 10 day without access
" 0 0
if [ $? = 0 ]
then
su $user -c 'find ~/.cache/ -atime +10 -exec rm -vrf {} \;'
su $user -c 'mkdir -p ~/.cache/'
su $user -c 'find ~/.var/app/*/cache/* -atime +10 -exec rm -vrf {} \;'
su $user -c 'find ~/snap/*/common/.cache/* -atime +10 -exec rm -vrf {} \;'
su $user -c 'find ~/.wine/drive_c/users/*/Temp/* -atime +10 -exec rm -vrf {} \;'
su $user -c 'rm -vrf ~/.cache/thumbnails/* 2>/dev/null'
su $user -c 'rm -vrf ~/.thumbs/* 2>/dev/null'
su $user -c 'rm -vrf ~/.thumbnails/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/pip/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/mozilla/firefox/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/flatpak/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/fontconfig/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/mesa_shader_cache/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/dolphin/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/kwin/* 2>/dev/null'
su $user -c 'rm -vrf ~/.cache/openSUSE/* 2>/dev/null'
fi
fi
dialog --title "remove tmp, trash or logs files" --yesno "You can remove tmp files, if you want" 0 0
if [ $? = 0 ]
then
dialog --title "tmp" --yesno "Clean temps folders:
- global tmp directory
" 0 0
if [ $? = 0 ]
then
rm -vrf /tmp/* 2>/dev/null
rm -vrf /var/tmp/* 2>/dev/null
fi
dialog --title "bash history and logs files" --yesno "
- Remove bash history
- remove tmux and godot games logs files
" 0 0
if [ $? = 0 ]
then
su $user -c 'rm -vrf ~/tmux-* 2>/dev/null'
su $user -c 'rm -vrf ~/.local/share/godot/app_userdata/*/logs/* 2>/dev/null'
su $user -c 'rm -vrf ~/.local/share/godot/app_userdata/*/mono/mono_logs/* 2>/dev/null'
su $user -c 'rm -vrf ~/.bash_history 2>/dev/null'
fi
dialog --title "trash" --yesno "Clean trash?" 0 0
if [ $? = 0 ]
then
su $user -c 'rm -vrf ~/.local/share/Trash/* 2>/dev/null'
su $user -c 'mkdir -p ~/.local/share/Trash/info/'
su $user -c 'mkdir -p ~/.local/share/Trash/files/'
fi
fi
dialog --title "thank you" --msgbox "thank you to use my script, you can share it with your friends. if you think that they need it. Bye!" 0 0
clear