1
0
Fork 0
dotfiles/.local/libexec/cron/trash-prune

50 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
IFS='
'
days=${1:-7}
types=${2:-ext2,ext3,ext4,xfs,btrfs,vfat,nfs}
getfsroot() {
printf "%s" "$(df "$1" --output=target | tail -1)"
}
list_trash_dirs() {
for mnt in $(findmnt -Py -t "$types"); do
eval "$mnt"
# shellcheck disable=2153
if [ "$TARGET" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ]; then
target=${XDG_DATA_HOME:-$HOME/.local/share}/Trash
elif [ "$TARGET" = "/" ]; then
target=/.Trash
else
target=${TARGET}/.Trash
fi
[ -w "$target" ] && { echo "$target"; return; }
echo
done
}
prune() {
[ -w "$1" ] || return 1
for file in "$1"/files/*; do
file=$(basename "$file")
[ "$file" = '*' ] && return
if [ ! -r "$1/info/$file" ] || [ ! "$(head -1 "$1/info/$file")" = "[Trash Info]" ]; then
[ -w "$1/info/$file" ] && rm -rf "$1/info/$file"
rm -rf "$1/files/$file"
continue
fi
if [ "$(date -ud "$days days ago" +%s)" -gt "$(date -ud "$(grep DeletionDate "$1/info/$file" | cut -d '=' -f2)" +%s)" ]; then
echo "$file too old!"
rm -rf "$1/info/$file"
rm -rf "$1/files/$file"
fi
done
}
for dir in $(list_trash_dirs); do
prune "$dir" || echo "Failed to prune $dir"
done