From acf411dc59c206a0b3e7ec993e1e56ee5af2ce5f Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Wed, 15 Feb 2023 23:20:24 +0000 Subject: [PATCH] Adds manual and more comprehenive options --- utils/free-up-disk-space.sh | 152 +++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 2 deletions(-) diff --git a/utils/free-up-disk-space.sh b/utils/free-up-disk-space.sh index 39e5523..9c4f192 100644 --- a/utils/free-up-disk-space.sh +++ b/utils/free-up-disk-space.sh @@ -22,6 +22,16 @@ # Licensed under MIT (C) Alicia Sykes 2022 # ###################################################################### +# Color Variables +CYAN_B='\033[1;96m' +YELLOW_B='\033[1;93m' +RED_B='\033[1;31m' +GREEN_B='\033[1;32m' +PLAIN_B='\033[1;37m' +RESET='\033[0m' +GREEN='\033[0;32m' +PURPLE='\033[0;35m' + function fuds_check_space () { convert_to_gb() { echo "$(($1/1048576))" ; } storage_used="$(df --output=used / | tail -n 1)" @@ -34,8 +44,6 @@ function fuds_check_space () { "($(convert_to_gb $storage_free) GB free)." } -fuds_check_space - function fuds_clean_pacman () { # Clean pacman cache sudo pacman -Scc @@ -56,6 +64,7 @@ function fuds_clean_apt () { } function fuds_remove_dead_snaps () { + # Remove disabled snaps snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do snap remove "$snapname" --revision="$revision" @@ -78,3 +87,142 @@ function fuds_clear_caches () { # Remove thumbnails for file viewers rm -rf ~/.cache/thumbnails/* } + +function fuds_remove_duplicates () { + # Find and prompt to delete duplicated files + fdupes . -G 10 --size -S -n -t -d +} + +function fuds_remove_broken () { + # Remove broken symlinks + find . -xtype l -delete + # Remove empty files + find . -type f -empty -delete + # Remove empty folders + find . -type d -empty -delete +} + +function fuds_show_help () { + echo "Usage: free-up-disk-space [OPTION]" + echo "Free up disk space on *nix based systems" + echo "" + echo "Options:" + echo " -h, --help Show this help message" + echo " -r, --run Run all tasks" + echo " -p, --pacman Clean pacman cache and remove orphaned packages" + echo " -f, --flatpak Remove unused Flatpak packages and delete cache" + echo " -a, --apt Remove obsolete packages" + echo " -s, --snaps Remove disabled snaps" + echo " -j, --journal Configure journal logs" + echo " -t, --trash Empty trash" + echo " -c, --caches Remove thumbnails and other caches" + echo " -d, --dups Find and delete duplicated files" + echo " -b, --broken Remove broken symlinks and empty files + folders" + echo "" + echo "Examples:" + echo " free-up-disk-space -r" + echo " free-up-disk-space -p -f -a -s -j -t -c -d -b" +} + +function free_up_disk_space () { + # Check available disk space + fuds_check_space + + # Prompt to clean pacman cache + echo -e "\n${CYAN_B}Would you like to clean pacman cache? (y/N)${RESET}" + read -n 1 -r ans_clean_pacman + if [[ $ans_clean_pacman =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_clean_pacman + fi + + # Prompt to remove unused Flatpak packages + echo -e "\n${CYAN_B}Would you like to remove unused Flatpak packages? (y/N)${RESET}" + read -n 1 -r ans_clean_flatpak + if [[ $ans_clean_flatpak =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_clean_flatpak + fi + + # Prompt to remove obsolete packages + echo -e "\n${CYAN_B}Would you like to remove obsolete packages? (y/N)${RESET}" + read -n 1 -r ans_clean_apt + if [[ $ans_clean_apt =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_clean_apt + fi + + # Prompt to remove disabled snaps + echo -e "\n${CYAN_B}Would you like to remove disabled snaps? (y/N)${RESET}" + read -n 1 -r ans_remove_dead_snaps + if [[ $ans_remove_dead_snaps =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_remove_dead_snaps + fi + + # Prompt to configure journal logs + echo -e "\n${CYAN_B}Would you like to configure journal logs? (y/N)${RESET}" + read -n 1 -r ans_journal_configure + if [[ $ans_journal_configure =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_journal_configure + fi + + # Prompt to empty trash + echo -e "\n${CYAN_B}Would you like to empty trash? (y/N)${RESET}" + read -n 1 -r ans_empty_trash + if [[ $ans_empty_trash =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_empty_trash + fi + + # Prompt to remove thumbnails and other caches + echo -e "\n${CYAN_B}Would you like to remove thumbnails and other caches? (y/N)${RESET}" + read -n 1 -r ans_clear_caches + if [[ $ans_clear_caches =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_clear_caches + fi + + # Prompt to find and delete duplicated files + echo -e "\n${CYAN_B}Would you like to find and delete duplicated files? (y/N)${RESET}" + read -n 1 -r ans_remove_duplicates + if [[ $ans_remove_duplicates =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_remove_duplicates + fi + + # Prompt to remove broken symlinks and empty files + folders + echo -e "\n${CYAN_B}Would you like to remove broken symlinks and empty files + folders? (y/N)${RESET}" + read -n 1 -r ans_remove_broken + if [[ $ans_remove_broken =~ ^[Yy]$ ]] || [[ $AUTO_YES = true ]] ; then + fuds_remove_broken + fi +} + +# Determine if file is being run directly or sourced +([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] || + [[ -n $KSH_VERSION && $(cd "$(dirname -- "$0")" && + printf '%s' "${PWD%/}/")$(basename -- "$0") != "${.sh.file}" ]] || + [[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)) && sourced=1 || sourced=0 + +# If script being called directly, invoke transfer or show help +if [ $sourced -eq 0 ]; then + if [[ $@ == *"--help"* ]]; then + fuds_show_help + elif [[ $@ == *"--pacman"*]]; then + fuds_clean_pacman + elif [[ $@ == *"--flatpak"*]]; then + fuds_clean_flatpak + elif [[ $@ == *"--apt"*]]; then + fuds_clean_apt + elif [[ $@ == *"--snaps"*]]; then + fuds_remove_dead_snaps + elif [[ $@ == *"--journal"*]]; then + fuds_journal_configure + elif [[ $@ == *"--trash"*]]; then + fuds_empty_trash + elif [[ $@ == *"--caches"*]]; then + fuds_clear_caches + elif [[ $@ == *"--dups"*]]; then + fuds_remove_duplicates + elif [[ $@ == *"--broken"*]]; then + fuds_remove_broken + elif [[ $@ == *"--run"*]]; then + free_up_disk_space + else + free_up_disk_space + fi +fi