dotfiles/.local/bin/scripts/clean

106 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# Clean up files according to given parameters
# TODO filter recently accessed files
typeset -A _clean_map
_clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp [l]=/var/log [v]=/var/cache)
# TODO .mix - outdated version in socha
_clean_home=(.ant .autopsy .bundle .cargo .cpanm .docker .stack .hex .nix-defexpr .parallel .surf
#.cache
.yarn .node_modules .npm .pnpm-store .node-gyp .nv .electron .electron-gyp
.gradle .gradle-kotlin-dsl .java .kscript .konan .m2 .openjfx
.log luametatex-cache luatex-cache .texlive
.lesshst .python_history .rubberband.wisdom.d .yarnrc)
[[ $1 =~ "." ]] && local=pwd
[[ $1 =~ "i" ]] && i=-i
dir="$("$local" 2>/dev/null || echo "$HOME")"
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"
highlight "cleaning home"
for f in ${_clean_home[@]}
do file="$dir/$f"
test -f "$file" || test -d "$file" && echo "Removing superfluous $file" && rm -rf $i "$file"
done
find "$dir" -maxdepth 2 -not -name ".stfolder" -empty -printf "Removing empty %p\n" -delete
find -L -maxdepth 2 -type l -printf "Removing broken symlink %p\n" -delete 2>/dev/null
test -d /mnt/data/backups/mobile/ &&
mv -v $DATA/4*/backups/mobile/signal-* /mnt/data/backups/mobile/ 2>/dev/null
highlight "y :: recursively remove empty folders and files"
if [[ $1 =~ "y" ]]; then
find -name '.thumbnails' -printf "Pre-cleaning %p\n" -exec rm -r {} +
find \( -name ".stfolder" -o -name ".*keep" -o -name ".git" -o -name "tmp" -o -name ".nomedia" -o -name "__init__.py" -o -name "instalee" \) -prune -o -empty -printf "Removing empty %p\n" -exec rm -d {} +
fi
highlight "e :: remove downloaded emacs packages (rebuild takes minutes!)"
[[ $1 =~ "e" ]] &&
find $DOOMLOCALDIR -maxdepth 1 \( -name straight -o -type f \) -printf "Removing %p\n" -exec rm -rf {} +
for k in "${!_clean_map[@]}"; do
folder="${_clean_map[$k]}"
highlight "$k :: delete $folder"
[[ $1 =~ "$k" ]] && (test "$k" = "l" && sudo find "$folder" -type f -delete || sudo rm $i -rf "$folder") && echo "Deleted $folder"
done
highlight "s :: recursively remove logs"
[[ $1 =~ "s" ]] && find \( -name ".git" -o -name "src" -o -name "pkg" \) -prune -o \
\( \( -name "logs" -o -name "log" -o -name "crash-reports" -o -name ".auctex-auto" \) -prune -o \
-type f \( -name "*.log.gz" -o -name "*.log.[0-9]" -o -iname "*.log" -o -name "*.tuc" -o -name "*.tua" \) \) \
-print -exec rm -rI {} +
highlight "m :: recursively remove mac-files"
if [[ $1 =~ "m" ]]; then
find \( -name '__MACOSX' -o -name '._*' \) -print -exec rm -r {} +
find \( -iname '.spotlight*' -o -name 'System Volume Information' -o -name '.fseventsd' \) -print -exec rm -rI {} +
find -name '*.DS_Store' -delete
echo 'Removed DS Stores'
fi
highlight "d :: recursively remove development caches"
if [[ $1 =~ "d" ]]; then
# TODO some matches duplicate log clearing
find -maxdepth 1 \( -name "*.aux" -o -name "*.log" -o -name "*.t[uo][ca]" -o -name "*.out" \) -print -delete
find \( -name 'src' -o -name 'vendor' -o -name 'web' \) -prune -o \
-type d \( -name 'cache' $(echo $DIRS_GENERATED | sed 's|-x \([^ ]\+\)|-o -name \1|g') \) \
-print -exec rm $i -r {} + -prune
echo -n " " && highlight "build directories"
find \( -name ".git" -o -path "*/mixxx/build" -o -name "app" -o -name "Leaflet" -o -name "www" -o -name "src" \) \
-prune -o -name "build" -type d \
-print -exec rm -rI {} + -prune
# TODO consider gitignore / use git clean
fi
highlight "g :: clean gradle caches"
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 {} +
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
highlight "o :: optimize space extensively"
if [[ $1 =~ "o" ]]; then
sudo find $XDG_CACHE_HOME /var/cache /var/log /var/tmp -mindepth 1 -maxdepth 2 -atime +2 -exec rm -r {} + -prune
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:--1}" &&
sudo apt-get clean && sudo apt-get autoremove &&
echo "Cleaned apt"
export aptclean_last=$aptclean_cur
fi
sudo rm -rf "$XDG_DATA_HOME/baloo"
docker image prune
which yay &>/dev/null &&
yay -Sc --noconfirm
nix-collect-garbage -d 2>/dev/null &&
nix-store --optimize
fi
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"