2021-09-18 21:04:50 +00:00
|
|
|
#!/bin/bash
|
2021-11-25 13:11:14 +00:00
|
|
|
# Clean up files according to given parameters
|
2021-09-18 21:04:50 +00:00
|
|
|
|
|
|
|
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)
|
2022-01-05 16:53:06 +00:00
|
|
|
_clean_home=(.ant .autopsy .bundle .cache .cargo .cpanm .docker .electron .electron-gyp .gradle .gradle-kotlin-dsl .hex .java .kscript .konan .m2 .mix .nix-defexpr .node-gyp .node_modules .npm .pnpm-store .nv .openjfx .parallel .stack .surf .texlive .yarn luametatex-cache
|
|
|
|
.lesshst .python_history .rubberband.wisdom.d .yarnrc)
|
2021-09-18 21:04:50 +00:00
|
|
|
|
2021-11-25 13:11:14 +00:00
|
|
|
[[ $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"
|
2021-11-25 13:11:14 +00:00
|
|
|
|
2022-01-05 16:53:06 +00:00
|
|
|
highlight "cleaning home"
|
|
|
|
for f in ${_clean_home[@]}
|
2022-01-30 21:17:45 +00:00
|
|
|
do file="$dir/$f"
|
|
|
|
test -f "$file" || test -d "$file" && echo "Removing superfluous $file" && rm -rf $i "$file"
|
2021-09-18 21:04:50 +00:00
|
|
|
done
|
2021-12-09 11:27:33 +00:00
|
|
|
find "$dir" -maxdepth 2 -not -name ".stfolder" -empty -printf "Removing empty %p\n" -delete
|
2022-01-08 18:16:59 +00:00
|
|
|
find -L -maxdepth 2 -type l -printf "Removing broken symlink %p\n" -delete
|
2021-09-18 21:04:50 +00:00
|
|
|
|
2021-12-12 19:24:56 +00:00
|
|
|
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
|
2021-09-18 21:04:50 +00:00
|
|
|
|
|
|
|
for k in "${!_clean_map[@]}"; do
|
|
|
|
folder="${_clean_map[$k]}"
|
2021-12-12 19:24:56 +00:00
|
|
|
highlight "$k :: delete $folder"
|
2022-01-17 19:58:04 +00:00
|
|
|
[[ $1 =~ "$k" ]] && (test "$k" = "l" && sudo find "$folder" -type f -delete || sudo rm $i -rf "$folder") && echo "Deleted $folder"
|
2021-09-18 21:04:50 +00:00
|
|
|
done
|
|
|
|
|
2021-12-12 19:24:56 +00:00
|
|
|
highlight "s :: recursively remove logs"
|
2022-01-17 20:25:24 +00:00
|
|
|
[[ $1 =~ "s" ]] && find \( -name ".git" -o -name "src" -o -name "pkg" \) -prune -o \
|
2022-02-10 15:02:30 +00:00
|
|
|
\( \( -name "logs" -o -name "log" -o -name "crash-reports" -o -name ".auctex-auto" \) -prune -o \
|
2022-03-15 15:56:22 +00:00
|
|
|
-type f \( -name "*.log.gz" -o -name "*.log.[0-9]" -o -iname "*.log" -o -name "*.tuc" -o -name "*.tua" \) \) \
|
2021-12-13 13:33:20 +00:00
|
|
|
-print -exec rm -rI {} +
|
2021-12-12 19:24:56 +00:00
|
|
|
|
|
|
|
highlight "m :: recursively remove mac-files"
|
2021-11-11 18:46:29 +00:00
|
|
|
if [[ $1 =~ "m" ]]; then
|
2022-01-30 21:17:45 +00:00
|
|
|
find -name '__MACOSX' -print -exec rm -r {} +
|
|
|
|
find -iname '.spotlight*' -print -exec rm -rI {} +
|
2021-11-11 18:46:29 +00:00
|
|
|
find -name '*.DS_Store' -delete
|
|
|
|
fi
|
|
|
|
|
2021-12-13 13:33:20 +00:00
|
|
|
highlight "d :: recursively remove development caches"
|
|
|
|
if [[ $1 =~ "d" ]]; then
|
|
|
|
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
|
|
|
|
echo -n " " && highlight "build directories"
|
|
|
|
find \( -name ".git" -o -path "*/mixxx/build" -o -name "Leaflet" -o -name "www" -o -name "src" \) \
|
|
|
|
-prune -o -name "build" -type d \
|
|
|
|
-print -exec rm -rI {} + -prune
|
|
|
|
fi
|
|
|
|
highlight "g :: clean gradle caches"
|
2021-11-11 18:46:29 +00:00
|
|
|
if [[ $1 =~ "g" ]]; then
|
2021-11-25 13:11:14 +00:00
|
|
|
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
|
|
|
|
|
2021-12-12 19:24:56 +00:00
|
|
|
highlight "c :: clean electron caches"
|
2021-11-25 13:11:14 +00:00
|
|
|
[[ $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
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
if test -f "/var/log/apt/history.log"; then
|
|
|
|
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)
|
2022-02-10 15:02:30 +00:00
|
|
|
test "$aptclean_cur" -gt "${aptclean_last:--1}" &&
|
|
|
|
sudo apt-get clean && sudo apt-get autoremove &&
|
|
|
|
echo "Cleaned apt"
|
2021-09-18 21:04:50 +00:00
|
|
|
export aptclean_last=$aptclean_cur
|
|
|
|
fi
|
|
|
|
|
2021-12-12 19:24:56 +00:00
|
|
|
highlight "o :: optimize space extensively"
|
2021-09-18 21:04:50 +00:00
|
|
|
if [[ $1 =~ "o" ]]; then
|
2022-04-12 16:59:02 +00:00
|
|
|
sudo rm -rf "$XDG_DATA_HOME/baloo"
|
2021-09-18 21:04:50 +00:00
|
|
|
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"
|