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

42 lines
947 B
Plaintext
Raw Normal View History

#!/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
if [ "$TARGET" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ]; then
echo "${XDG_DATA_HOME:-$HOME/.local/share}/Trash"
elif [ "$TARGET" = "/" ]; then
echo "/.Trash"
else
echo "${TARGET}/.Trash"
fi
done
}
prune_dir() {
[ -w "$1" ] || return 1
for file in "$1"/info/*; do
[ "$(head -1 "$file")" = "[Trash Info]" ] || continue
if [ "$(date -ud "$days days ago" +%s)" -gt "$(date -ud "$(grep DeletionDate "$file" | cut -d '=' -f2)" +%s)" ]; then
file=$(basename "$file")
echo "$file too old!"
rm "$1/info/$file"
rm "$1/files/$file"
fi
done
}
for dir in $(list_trash_dirs); do
prune_dir $dir || echo "Not pruning $dir" >&2
done