dotfiles/.local/bin/scripts/clean

103 lines
4.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# Clean up files according to given parameters
2022-10-31 16:44:15 +00:00
# TODO filter recently accessed files
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)
2023-03-13 14:36:09 +00:00
# TODO .mix - outdated version in socha
2023-11-07 09:58:02 +00:00
_clean_home=(.ant .autopsy .bundle .cargo .cpanm .docker .stack .hex .nix-defexpr .parallel .surf .pub-cache
2023-11-04 18:08:39 +00:00
#.cache
2023-09-27 13:38:23 +00:00
.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
2023-03-12 21:35:11 +00:00
.lesshst .python_history .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"
2022-01-05 16:53:06 +00:00
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
2021-12-09 11:27:33 +00:00
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
2022-11-22 12:24:52 +00:00
test -d /mnt/data/backups/mobile/ &&
mv -v $DATA/4*/backups/mobile/signal-* /mnt/data/backups/mobile/ 2>/dev/null
2022-07-20 16:17:57 +00:00
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"
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"
done
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 \
\( \( -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 {} +
highlight "m :: recursively remove mac-files"
2021-11-11 18:46:29 +00:00
if [[ $1 =~ "m" ]]; then
2023-04-27 12:28:04 +00:00
find \( -name '__MACOSX' -o -name '._*' \) -print -exec rm -r {} +
2022-11-09 11:45:38 +00:00
find \( -iname '.spotlight*' -o -name 'System Volume Information' -o -name '.fseventsd' \) -print -exec rm -rI {} +
2021-11-11 18:46:29 +00:00
find -name '*.DS_Store' -delete
2023-03-13 14:36:09 +00:00
echo 'Removed DS Stores'
2021-11-11 18:46:29 +00:00
fi
2021-12-13 13:33:20 +00:00
highlight "d :: recursively remove development caches"
if [[ $1 =~ "d" ]]; then
2022-11-09 11:45:38 +00:00
# TODO some matches duplicate log clearing
find -maxdepth 1 \( -name "*.aux" -o -name "*.log" -o -name "*.t[uo][ca]" -o -name "*.out" \) -print -delete
2023-03-13 14:36:09 +00:00
find \( -name 'src' -o -name 'vendor' -o -name 'web' \) -prune -o \
2021-12-13 13:33:20 +00:00
-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"
2022-06-27 09:24:06 +00:00
find \( -name ".git" -o -path "*/mixxx/build" -o -name "app" -o -name "Leaflet" -o -name "www" -o -name "src" \) \
2021-12-13 13:33:20 +00:00
-prune -o -name "build" -type d \
-print -exec rm -rI {} + -prune
2022-06-27 09:24:06 +00:00
# TODO consider gitignore / use git clean
2021-12-13 13:33:20 +00:00
fi
highlight "g :: clean gradle caches"
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")" \
2022-06-27 09:24:06 +00:00
-type d -name "*Cache*" -print -exec rm $i -r {} + -prune
2021-11-11 18:46:29 +00:00
highlight "o :: optimize space extensively"
if [[ $1 =~ "o" ]]; then
2024-04-08 11:02:09 +00:00
sudo find /root/.cache $XDG_CACHE_HOME /var/cache /var/log /var/tmp -not -name CACHEDIR.TAG -mindepth 1 -maxdepth 2 -atime +2 -exec rm -r {} + -prune
2022-11-19 18:57:52 +00:00
2022-09-27 19:58:13 +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:--1}" &&
sudo apt-get clean && sudo apt-get autoremove &&
echo "Cleaned apt"
export aptclean_last=$aptclean_cur
fi
2022-04-12 16:59:02 +00:00
sudo rm -rf "$XDG_DATA_HOME/baloo"
2024-06-28 19:34:05 +00:00
# https://www.cyberciti.biz/faq/delete-remove-orphaned-unused-packages-arch-linux-pacman-command/
which pacman &>/dev/null &&
pacman -Qqdt | sudo pacman -Rsu -
2022-12-13 12:25:04 +00:00
which yay &>/dev/null &&
yay -Sc --noconfirm
nix-collect-garbage -d 2>/dev/null &&
nix-store --optimize
2024-06-28 19:34:05 +00:00
docker image prune
fi
2021-12-02 09:35:37 +00:00
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"