dotfiles/.local/bin/scripts/rd

31 lines
1.1 KiB
Bash
Executable File

#!/bin/sh -e
# Remove recursively safely
case "$1" in ([0-9]) depth=$1; shift;; esac
for f in "${@:-$PWD}"
do
test -w "$(dirname "$f")" && elevate="" || elevate=sudo
if test -d "$f"
then
if test -e "$f/.git" || test -e "$f/packed-refs"
then echo -n "Force delete git project $f? "
read answer
test "$answer" = "y" &&
$elevate rm -rf "$f" &&
continue
fi
# Clean empty files
$elevate find -name '.thumbnails' -printf 'Pre-cleaning %p\n' -exec rm -r {} +
$elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 3) \
\( -name '.stfolder' -o -name '.*keep' -o -name '*.py' -o -name 'nodelay.txt' -o -name '.git' -o -name 'tmp' -o -name '.nomedia' -o -name '__init__.py' -o -name '*ignore' -o -name 'instalee' \) \
\( -type d -o -type f \) -prune -o -empty -printf 'Removing empty %p\n' -exec rm -d {} +
# $elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 4) -type d -empty -name .stfolder -exec rm -div {} \;
test $# -eq 0 && exit $?
if test -e "$f"; then
echo -n "$f ($(ls -A "$f" | head -3 | paste -s -d' ')) " >&2 &&
$elevate rm -rI "$f"
fi
else $elevate rm -i "$f"
fi
done