bin: utilities for cleaning up files

This commit is contained in:
xeruf 2022-06-27 11:24:06 +02:00
parent 5c17af6d6f
commit db1e39b9e8
4 changed files with 20 additions and 10 deletions

View File

@ -21,7 +21,7 @@ find "$dir" -maxdepth 2 -not -name ".stfolder" -empty -printf "Removing empty %p
find -L -maxdepth 2 -type l -printf "Removing broken symlink %p\n" -delete find -L -maxdepth 2 -type l -printf "Removing broken symlink %p\n" -delete
highlight "e :: recursively remove empty folders and files" highlight "e :: recursively remove empty folders and files"
[[ $1 =~ "e" ]] && find -empty -not -name ".stfolder" -empty -printf "Removing empty %p\n" -delete [[ $1 =~ "e" ]] && find \( -name ".stfolder" -o -name ".*keep" -o -name ".git" \) -prune -o -empty -printf "Removing empty %p\n" -exec rm -d {} +
for k in "${!_clean_map[@]}"; do for k in "${!_clean_map[@]}"; do
folder="${_clean_map[$k]}" folder="${_clean_map[$k]}"
@ -49,9 +49,10 @@ if [[ $1 =~ "d" ]]; then
-type d \( -name 'cache' $(echo $DIRS_GENERATED | sed 's|-x \([^ ]\+\)|-o -name \1|g') \) \ -type d \( -name 'cache' $(echo $DIRS_GENERATED | sed 's|-x \([^ ]\+\)|-o -name \1|g') \) \
-print -exec rm $i -r {} + -prune -print -exec rm $i -r {} + -prune
echo -n " " && highlight "build directories" echo -n " " && highlight "build directories"
find \( -name ".git" -o -path "*/mixxx/build" -o -name "Leaflet" -o -name "www" -o -name "src" \) \ 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 \ -prune -o -name "build" -type d \
-print -exec rm -rI {} + -prune -print -exec rm -rI {} + -prune
# TODO consider gitignore / use git clean
fi fi
highlight "g :: clean gradle caches" highlight "g :: clean gradle caches"
if [[ $1 =~ "g" ]]; then if [[ $1 =~ "g" ]]; then
@ -61,7 +62,7 @@ fi
highlight "c :: clean electron caches" highlight "c :: clean electron caches"
[[ $1 =~ "c" ]] && find "$dir$(expr "${XDG_CONFIG_HOME/$HOME}" \| "/.config")" \ [[ $1 =~ "c" ]] && find "$dir$(expr "${XDG_CONFIG_HOME/$HOME}" \| "/.config")" \
-type d -name "*Cache" -print -exec rm $i -r {} + -prune -type d -name "*Cache*" -print -exec rm $i -r {} + -prune
if test -f "/var/log/apt/history.log"; then if test -f "/var/log/apt/history.log"; then
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l) aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)

View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
# Find and remove sizeable files # Find and remove sizeable files
find -size +5M -print -exec rm -I {} + find -size +${1:-50}M -exec rm -vi {} +

View File

@ -1,3 +1,12 @@
#!/bin/sh #!/bin/sh -e
# Remove one level of folders # Flatten folder hierarchy
find -mindepth 1 -maxdepth 1 -exec sh -c 'mv -nv {}/* . && rm -dv {}' \; # Args: depth
find -mindepth 2 -maxdepth ${depth:-2} -depth -type d "$@" | while read folder; do
newname="$(echo "$folder" | sed 's|^./||;s|/|_|g')"
mv -v "$folder" "$newname"
parent="$(dirname "$folder")"
test "$(basename "$folder")" = "$(basename "$parent")" &&
rm -d "$parent" &&
mv -v "$newname" "$parent"
done
find -maxdepth ${depth:-2} -empty -type d -delete

View File

@ -1,11 +1,11 @@
#!/bin/sh -e #!/bin/sh -e
# Delete files under current or given path which exist elsewhere as listed in the locate database # Delete files under current or given path which exist elsewhere as listed in the locate database
# Matches first by name, then by checksum (currently inefficiently via md5) # Matches first by name, then by checksum (currently inefficiently via md5)
case "$1" in ([0-9]) size=$1; shift;; esac case $1 in ([0-9]|[0-9][0-9]) threshold=$1; shift;; esac
find "$@" -size +${size:-5}M -type f -exec sh -c "IFS=$'\n'"' find "$@" -size +${threshold:-50}M -type f -exec sh -c "IFS=$'\n'"'
filepath="{}" filepath="{}"
target="$(basename "$filepath" | synct-unarchive)" target="$(basename "$filepath" | synct-unarchive)"
highlight "$filepath" highlight "$filepath"
for existing in $(locate -b "$target") for existing in $(locate -b "$target")
do test "$(realpath "$target")" != "$(realpath "$existing")" -a -f "$existing" -a "$(md5sum "$existing" | cut -d\ -f1)" = "$(md5sum "$filepath" | cut -d\ -f1)" && echo "Found duplicate at $existing" && rm -vi "$filepath" && break do test "$(realpath "$target")" != "$(realpath "$existing")" -a -f "$existing" && test "$(md5sum "$existing" | cut -d\ -f1)" = "$(md5sum "$filepath" | cut -d\ -f1)" && echo "Found duplicate at $existing" && rm -vi "$filepath" && break
done' \; done' \;