dotfiles/.local/bin/scripts/clean

78 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# Clean up files according to given parameters
typeset -A _clean_map
2021-11-11 18:46:29 +00:00
_clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp [l]=/var/log [v]=/var/cache)
# Directories to always clean
2021-11-10 16:42:28 +00:00
_clean_folders=(~/.ant ~/.autopsy ~/.bundle ~/.cache ~/.cargo ~/.cpanm ~/.docker ~/.electron ~/.electron-gyp ~/.gradle ~/.gradle-kotlin-dsl ~/.hex ~/.java ~/.kscript ~/.konan ~/.m2 ~/.mix ~/.nix-defexpr ~/.node-gyp ~/.npm ~/.nv ~/.openjfx ~/.parallel ~/.stack ~/.surf ~/.texlive ~/.yarn luametatex-cache
2021-11-09 04:37:07 +00:00
~/.lesshst ~/.rubberband.wisdom.d ~/.yarnrc)
[[ $1 =~ "." ]] && local=pwd
[[ $1 =~ "i" ]] && i=-i
dir="$("$local" 2>/dev/null || echo "$HOME")"
2021-12-02 09:35:37 +00:00
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"
highlight "home"
for f in ${_clean_folders[@]}
do "$local" >/dev/null 2>&1 && f="$(echo "$f" | sed "s|$HOME|$($loc)|")"
2021-12-13 12:56:42 +00:00
test -f $f || test -d $f && echo "Removing empty $f" && rm -rf $i $f
done
2021-12-09 11:27:33 +00:00
find "$dir" -maxdepth 2 -not -name ".stfolder" -empty -printf "Removing empty %p\n" -delete
highlight "e :: recursively remove empty folders and files"
2021-12-09 11:27:33 +00:00
[[ $1 =~ "e" ]] && find -empty -not -name ".stfolder" -empty -printf "Removing empty %p\n" -delete
for k in "${!_clean_map[@]}"; do
folder="${_clean_map[$k]}"
highlight "$k :: delete $folder"
[[ $1 =~ "$k" ]] && sudo rm $i -rf "$folder" && echo "Deleted $folder"
done
highlight "s :: recursively remove logs"
[[ $1 =~ "s" ]] && find \( -name "logs" -prune -o -name "crash-reports" -prune -o -iname "*log.txt" -o -name "*.log.*" -o -iname "*.log" \) -print -exec rm -rI {} +
highlight "m :: recursively remove mac-files"
2021-11-11 18:46:29 +00:00
if [[ $1 =~ "m" ]]; then
find -iname '.spotlight*' -exec rm -rI {} +
find -name '*.DS_Store' -delete
fi
highlight "g :: clean gradle"
2021-11-11 18:46:29 +00:00
if [[ $1 =~ "g" ]]; then
find "${GRADLE_USER_HOME:-$HOME/.gradle}" -mindepth 1 -maxdepth 1 -type d -print -exec rm $i -r {} +
find $PROJECTS -name .gradle -print -exec rm $i -r {} +
2021-11-11 18:46:29 +00:00
fi
highlight "c :: clean electron caches"
[[ $1 =~ "c" ]] && find "$dir$(expr "${XDG_CONFIG_HOME/$HOME}" \| "/.config")" \
-type d -name "*Cache" -print -exec rm $i -r {} + -prune
2021-11-11 18:46:29 +00:00
if test -f "/var/log/apt/history.log"; then
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)
test "$aptclean_cur" -gt "$aptclean_last" && aptclean && echo "Cleaned apt"
export aptclean_last=$aptclean_cur
fi
highlight "d :: recursively remove development caches"
2021-09-29 16:38:57 +00:00
if [[ $1 =~ "d" ]]; then
2021-11-17 14:56:16 +00:00
find -name "src" -prune -o \
-type d \( -name 'cache' $(echo $DIRS_GENERATED | sed 's|-x \([^ ]\+\)|-o -name \1|g') \) \
-print -exec rm $i -r {} + -prune
2021-09-29 16:38:57 +00:00
echo -n " " && highlight "build directories"
2021-11-10 16:42:28 +00:00
find $DATA/2-standards/dev/aur -mindepth 2 -maxdepth 2 -type d -not -name ".*" \
-print -exec sudo rm -rI {} +
find \( -name ".git" -o -path "*/mixxx/build" -o -name "Leaflet" -o -name "www" -o -name "src" \) \
-prune -o -name "build" -type d \
2021-09-29 16:38:57 +00:00
-print -exec rm -rI {} + -prune
fi
highlight "o :: optimize space extensively"
if [[ $1 =~ "o" ]]; then
which yay &>/dev/null && yay -Sc --noconfirm
nix-collect-garbage -d
nix-store --optimize
fi
2021-12-02 09:35:37 +00:00
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"