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

47 lines
1.1 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
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() {
[ -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