From 2169bb0d8f1fb07b4a9a418568ab61d2177ae83b Mon Sep 17 00:00:00 2001 From: eshanized Date: Sun, 22 Dec 2024 01:17:17 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(=5Fscript):=20for=20misc=20?= =?UTF-8?q?operation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/local/bin/snigdhaos-cleanup.sh | 112 +++++++++++++++++++++++++++++ usr/local/bin/uninstall-package.sh | 53 ++++++++++++++ usr/local/bin/update-snigdha-os.sh | 38 ++++++++++ 3 files changed, 203 insertions(+) create mode 100644 usr/local/bin/snigdhaos-cleanup.sh create mode 100644 usr/local/bin/uninstall-package.sh create mode 100644 usr/local/bin/update-snigdha-os.sh diff --git a/usr/local/bin/snigdhaos-cleanup.sh b/usr/local/bin/snigdhaos-cleanup.sh new file mode 100644 index 0000000..8a6016b --- /dev/null +++ b/usr/local/bin/snigdhaos-cleanup.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +# Author : Eshan Roy +# Author URL : https://eshanized.github.io/ + +set -e # Exit immediately if a command exits with a non-zero status + +# Colors +RED="\033[1;31m" +GREEN="\033[1;32m" +YELLOW="\033[1;33m" +BLUE="\033[1;34m" +RESET="\033[0m" + +# Function to display error message and exit +error_handler() { + echo -e "\n${RED}๐Ÿšจ An error occurred during the cleanup process.${RESET}" + echo -e "${RED}Error on line $1. Please check your system and try again.${RESET}" + exit 1 +} + +# Trap errors and call error_handler +trap 'error_handler $LINENO' ERR + +# Ensure the script is run as root for certain actions +if [ "$EUID" -ne 0 ]; then + echo -e "${RED}โŒ Please run this script as root or with sudo.${RESET}" + exit 1 +fi + +echo -e "${YELLOW}โš ๏ธ WARNING: This script will permanently delete unnecessary files, logs, and caches to free up disk space.${RESET}" +echo -e "${BLUE}๐Ÿ‘‰ The following will be cleaned:${RESET}" +echo -e "${GREEN} - Package cache${RESET}" +echo -e "${GREEN} - Crash reports${RESET}" +echo -e "${GREEN} - Application logs${RESET}" +echo -e "${GREEN} - Application caches${RESET}" +echo -e "${GREEN} - Trash${RESET}" +read -p "$(echo -e "${YELLOW}โ“ Are you sure you want to proceed? [y/N]: ${RESET}")" CONFIRM +if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then + echo -e "${RED}โŒ Cleanup operation cancelled.${RESET}" + exit 0 +fi + +# Function to calculate space freed +calculate_space_freed() { + local before_size + local after_size + + before_size=$(du -sh / | awk '{print $1}') + + # Running cleanup functions + clean_package_cache + clean_crash_reports + clean_application_logs + clean_application_caches + clean_trash + + after_size=$(du -sh / | awk '{print $1}') + + echo -e "${GREEN}Space freed: $before_size -> $after_size${RESET}" +} + +echo -e "${BLUE}๐Ÿงน Starting cleanup process...${RESET}" + +# Function to clean package cache +clean_package_cache() { + echo -e "${YELLOW}โš ๏ธ Deleting package cache (this will remove old cached packages and free up disk space)...${RESET}" + sudo pacman -Sc --noconfirm || { echo -e "${RED}โŒ Failed to clean package cache.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Package cache cleaned.${RESET}" +} + +# Function to clean crash reports +clean_crash_reports() { + echo -e "${YELLOW}โš ๏ธ Deleting crash reports (this will remove saved system crash data)...${RESET}" + CRASH_DIR="/var/lib/systemd/coredump" + if [ -d "$CRASH_DIR" ]; then + sudo rm -rf "$CRASH_DIR"/* || { echo -e "${RED}โŒ Failed to clean crash reports.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Crash reports cleaned.${RESET}" + else + echo -e "${GREEN}โœ… No crash reports found.${RESET}" + fi +} + +# Function to clean application logs +clean_application_logs() { + echo -e "${YELLOW}โš ๏ธ Truncating application logs (this will reset log files to 0 bytes)...${RESET}" + LOG_DIR="/var/log" + sudo find "$LOG_DIR" -type f -name "*.log" -exec truncate -s 0 {} \; || { echo -e "${RED}โŒ Failed to clean application logs.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Application logs cleaned.${RESET}" +} + +# Function to clean application caches +clean_application_caches() { + echo -e "${YELLOW}โš ๏ธ Deleting application caches (this will remove all cache files from user directories)...${RESET}" + CACHE_DIR="/home/*/.cache" + sudo rm -rf $CACHE_DIR/* || { echo -e "${RED}โŒ Failed to clean application caches.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Application caches cleaned.${RESET}" +} + +# Function to empty trash +clean_trash() { + echo -e "${YELLOW}โš ๏ธ Emptying trash (this will delete all items in the trash folders)...${RESET}" + TRASH_DIR="/home/*/.local/share/Trash" + sudo rm -rf $TRASH_DIR/* || { echo -e "${RED}โŒ Failed to empty trash.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Trash emptied.${RESET}" +} + +# Calculate space freed before and after cleanup +calculate_space_freed + +echo -e "\n${GREEN}โœ… Cleanup completed successfully!${RESET}" +exit 0 diff --git a/usr/local/bin/uninstall-package.sh b/usr/local/bin/uninstall-package.sh new file mode 100644 index 0000000..6ca553f --- /dev/null +++ b/usr/local/bin/uninstall-package.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Author : Eshan Roy +# Author URL : https://eshanized.github.io/ + +set -e # Exit immediately if a command exits with a non-zero status + +# Function to display error message and exit +error_handler() { + echo -e "\n๐Ÿšจ An error occurred during the uninstallation process." + echo "Error on line $1. Please check your system and try again." + exit 1 +} + +# Trap errors and call error_handler +trap 'error_handler $LINENO' ERR + +# Ensure the script is run as root +if [ "$EUID" -ne 0 ]; then + echo "โŒ Please run this script as root or with sudo." + exit 1 +fi + +# Check if a package name is provided +if [ -z "$1" ]; then + echo "โŒ No package specified." + echo "Usage: $0 " + exit 1 +fi + +PACKAGE=$1 + +# Confirm with the user +read -p "Are you sure you want to uninstall $PACKAGE and remove its cache? [y/N]: " CONFIRM +if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then + echo "โŒ Operation cancelled." + exit 0 +fi + +# Uninstall the package and its dependencies +echo "๐Ÿ“ฆ Uninstalling $PACKAGE..." +sudo pacman -Rns $PACKAGE --noconfirm || { echo "โŒ Failed to uninstall $PACKAGE."; exit 1; } + +# Remove the package's cache +echo "๐Ÿ—‘๏ธ Removing cache for $PACKAGE..." +sudo pacman -Sc --noconfirm || echo "โ— Cache removal skipped." + +# Clean up orphaned packages +echo "๐Ÿงน Cleaning up orphaned packages..." +sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo "No orphaned packages to remove." + +echo -e "\nโœ… Package $PACKAGE successfully uninstalled!" +exit 0 diff --git a/usr/local/bin/update-snigdha-os.sh b/usr/local/bin/update-snigdha-os.sh new file mode 100644 index 0000000..245d373 --- /dev/null +++ b/usr/local/bin/update-snigdha-os.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Author : Eshan Roy +# Author URL : https://eshanized.github.io/ + +# Shell script to update all Snigdha OS packages with error handling + +set -e # Exit immediately if a command exits with a non-zero status + +# Function to display error message and exit +error_handler() { + echo -e "\n๐Ÿšจ An error occurred during the update process." + echo "Error on line $1. Please check your system and try again." + exit 1 +} + +# Trap errors and call error_handler +trap 'error_handler $LINENO' ERR + +# Ensure the script is run as root +if [ "$EUID" -ne 0 ]; then + echo "โŒ Please run this script as root or with sudo." + exit 1 +fi + +# Update the package database and upgrade packages +echo "๐Ÿ”„ Updating the package database..." +sudo pacman -Sy || { echo "โŒ Failed to update the package database."; exit 1; } + +echo "๐Ÿ“ฆ Upgrading installed packages..." +sudo pacman -Su --noconfirm || { echo "โŒ Failed to upgrade packages."; exit 1; } + +# Clean up orphaned packages +echo "๐Ÿงน Cleaning up orphaned packages..." +sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo "No orphaned packages to remove." + +echo -e "\nโœ… System successfully updated!" +exit 0