From 5ce5c4bea9ff0a19e2c0cfe5098754479a2448e9 Mon Sep 17 00:00:00 2001 From: vijay Date: Sat, 14 Jun 2025 08:53:04 +0000 Subject: [PATCH] Add disk_cleanup_linux --- disk_cleanup_linux | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 disk_cleanup_linux diff --git a/disk_cleanup_linux b/disk_cleanup_linux new file mode 100644 index 0000000..fa7404d --- /dev/null +++ b/disk_cleanup_linux @@ -0,0 +1,82 @@ +#!/bin/bash +# + +# Function to list directories +list_directories() { + local dir=$1 + echo "Listing directories under: $dir" + du -h --max-depth=1 "$dir" 2>/dev/null | sort -hr +} + +# Function to delete a directory +delete_directory() { + local dir_to_delete=$1 + echo "Are you sure you want to delete the directory: $dir_to_delete? (y/n)" + read -r confirmation + if [[ $confirmation == "y" ]]; then + rm -rf "$dir_to_delete" + echo "Deleted: $dir_to_delete" + else + echo "Deletion cancelled." + fi +} + +# Function to list largest files in a directory +list_largest_files() { + local dir=$1 + echo "Listing largest files in: $dir" + find "$dir" -type f -exec du -h {} + | sort -hr | head -n 10 +} + +# Function to display the menu +display_menu() { + echo "Menu Options:" + echo "1. List directories" + echo "2. Delete a directory" + echo "3. Dig into a directory to find largest files" + echo "4. Quit" +} + +# Main function to manage disk cleanup +disk_cleanup() { + local current_dir="/home" + + while true; do + display_menu + read -r choice + + case $choice in + 1) + list_directories "$current_dir" + ;; + 2) + echo "Enter the directory path to delete (relative to $current_dir):" + read -r user_input + if [[ -d "$current_dir/$user_input" ]]; then + delete_directory "$current_dir/$user_input" + else + echo "Invalid directory. Please try again." + fi + ;; + 3) + echo "Enter the directory path to dig into (relative to $current_dir):" + read -r user_input + if [[ -d "$current_dir/$user_input" ]]; then + list_largest_files "$current_dir/$user_input" + else + echo "Invalid directory. Please try again." + fi + ;; + 4) + echo "Exiting..." + break + ;; + *) + echo "Invalid option. Please try again." + ;; + esac + done +} + +# Start the disk cleanup process +disk_cleanup