Created
February 14, 2024 01:30
-
-
Save loopyd/b901095a52952ff9e175db67f0fc1184 to your computer and use it in GitHub Desktop.
[bash] File/Directory tidy tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# autoclean.sh | Version 0.0.1 | Author: @loopyd | |
# | |
# This script is used to clean up files and folders that are not needed on your linux system | |
# Directories to clean are stored in an associative array with the key being the directory and the value being: | |
# a comma separated list of file or folder names and types. These are compatible arguments for the | |
# find command. The script will search for these files and folders and remove them. | |
# | |
# DATA LOSS WARNING: This script will remove files and folders from your system. Please be careful with it. | |
# I accept no legal responsibility for any damage caused by using or misusing this script. Change the configuration | |
# at your own risk. | |
# Put your directories that you want to autoclean by file or folder name and type here. | |
declare -A files=( | |
["/usr"]="*.pyc,f,__pycache__,d,*.tmp,f" | |
["/home"]="*.pyc,f,__pycache__,d,*.tmp,f" | |
["/var/log"]="*.log,f,*.tmp,f" | |
["/tmp"]="*,f,*,d" | |
) | |
# Print human readable size | |
function human_readable() { | |
local total=$1 | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="KB" | |
fi | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="MB" | |
fi | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="GB" | |
fi | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="TB" | |
fi | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="PB" | |
fi | |
if [[ $total -gt 1024 ]]; then | |
total=$((total/1024)) | |
unit="EB" | |
fi | |
printf "%d %s" $total $unit | |
} | |
total_size=0 | |
declare -A found=() | |
for folder in "${!files[@]}"; do | |
if [ ! -d "$folder" ]; then | |
continue | |
fi | |
declare -a match_arr=() | |
IFS=',' read -r -a match_arr<<<"${files[$folder]}" | |
while [ ${#match_arr[@]} -ne 0 ]; do | |
match="${match_arr[0]}" | |
type="${match_arr[1]}" | |
match_arr=("${match_arr[@]:2}") | |
list_items=() | |
readarray -t list_items<<<"$(sudo find "$folder" -name "$match" -type "$type")" | |
item_count=${#list_items[@]} | |
if [ $item_count -eq 0 ]; then | |
continue | |
fi | |
current_item=0 | |
current_size=0 | |
for item in "${list_items[@]}"; do | |
if [ -d "$item" ]; then | |
item_size=$(sudo du -s -b "$item" | awk '{print $1}') | |
sudo rm -rf "$item" &>/dev/null | |
elif [ -f "$item" ]; then | |
item_size=$(stat -c %s "$item") | |
sudo rm -f "$item" &>/dev/null | |
else | |
continue | |
fi | |
found+=([$item]="$item_size") | |
current_item=$((current_item+1)) | |
current_size=$((current_size+item_size)) | |
total_size=$((total_size+item_size)) | |
printf "%s for %s of type %s | Progress: %d/%d | Reclaimed: %s/%s%*s\r" "$folder" "$match" "$type" $current_item $item_count "$(human_readable $current_size)" "$(human_readable $total_size)" "20" " " | |
done | |
done | |
done | |
printf "%*s\r" "$(tput cols)" " " | |
echo "${#found[@]} items removed | $(human_readable $total_size) reclaimed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment