1
0
Fork 0
dotfiles/.local/bin/trash-restore

33 lines
973 B
Plaintext
Raw Normal View History

#!/bin/sh
set -e
2023-11-21 15:17:44 +01:00
getfsroot() {
2023-02-07 17:19:53 +01:00
printf "%s" "$(df "$1" --output=target | tail -1)"
}
list() {
2023-02-07 17:19:53 +01:00
# 1st arg is trash files directory
# 2nd arg is directory to match trash files for
2023-11-16 14:06:50 +01:00
[ ! "$(command ls -A "$1")" ] && exit 1
while read -r file; do
filepath=$(grep Path "$file" | cut -d '=' -f2) || continue
echo "$filepath" | grep -qP "^$2/[^/]+$" || continue
printf "%s %s %s\n" "$(basename "$file")" "$filepath" "$(date -d "$(grep DeletionDate "$file" | cut -d '=' -f2)" +'%x %X')"
done <<EOF
$(grep -l '[Trash Info]' "$1"/* "$1"/.* 2>/dev/null)
EOF
}
2023-11-21 15:17:44 +01:00
fsroot="$(getfsroot "$PWD")"
2023-11-16 14:06:50 +01:00
if [ "$fsroot" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ]; then
basedir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash"
else
2023-02-07 17:19:53 +01:00
basedir="${fsroot}/.Trash"
2023-11-16 14:06:50 +01:00
fi
2023-11-21 15:17:44 +01:00
sel="$(list "$basedir/info" "$PWD" | fzf)"
2023-11-16 14:06:50 +01:00
file="$basedir/files/$(echo "$sel" | cut -d '' -f1)"
dest="$(echo "$sel" | cut -d '' -f3)"
mv -ib "$file" "$dest"
rm "$basedir/info/$(echo "$sel" | cut -d '' -f1)"