~kris/dots

srice

srice/.local/bin/system/trash -rw-r--r-- 3.0 KiB
e98f3b03 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh

# Simple trash bin - moves files to ~/.local/share/Trash instead of deleting
# Usage: trash <file1> [file2] ...
# trash -l            list trashed files
# trash -e            empty trash permanently
# trash -r <name>     restore file to original location

TRASH_DIR="$HOME/.local/share/Trash"
TRASH_FILES="$TRASH_DIR/files"
TRASH_INFO="$TRASH_DIR/info"

mkdir -p "$TRASH_FILES" "$TRASH_INFO"

case "$1" in
    -l|--list)
        if [ -z "$(ls -A "$TRASH_FILES" 2>/dev/null)" ]; then
            echo "trash is empty"
            exit 0
        fi
        echo "TRASHED FILES:"
        for info in "$TRASH_INFO"/*.trashinfo; do
            [ -f "$info" ] || continue
            name=$(basename "$info" .trashinfo)
            origin=$(grep "^Path=" "$info" | cut -d= -f2-)
            date=$(grep "^DeletionDate=" "$info" | cut -d= -f2-)
            printf "  %-40s  %s  %s\n" "$name" "$date" "$origin"
        done
        echo
        du -sh "$TRASH_FILES" | awk '{print "Total: " $1}'
        ;;
    -e|--empty)
        if [ -z "$(ls -A "$TRASH_FILES" 2>/dev/null)" ]; then
            echo "trash is already empty"
            exit 0
        fi
        size=$(du -sh "$TRASH_FILES" | awk '{print $1}')
        printf "permanently delete all trashed files (%s)? [y/N] " "$size"
        read -r ans
        case "$ans" in
            y|Y) rm -rf "$TRASH_FILES"/* "$TRASH_INFO"/*; echo "trash emptied" ;;
            *) echo "aborted" ;;
        esac
        ;;
    -r|--restore)
        shift
        [ -z "$1" ] && echo "usage: trash -r <name>" && exit 1
        info="$TRASH_INFO/$1.trashinfo"
        file="$TRASH_FILES/$1"
        if [ ! -f "$info" ] || [ ! -e "$file" ]; then
            echo "not found in trash: $1"
            exit 1
        fi
        origin=$(grep "^Path=" "$info" | cut -d= -f2-)
        if [ -e "$origin" ]; then
            echo "destination already exists: $origin"
            exit 1
        fi
        mkdir -p "$(dirname "$origin")"
        mv "$file" "$origin" && rm "$info"
        echo "restored: $origin"
        ;;
    -h|--help)
        echo "usage: trash <files>        move files to trash"
        echo "       trash -l             list trashed files"
        echo "       trash -e             empty trash"
        echo "       trash -r <name>      restore file"
        ;;
    *)
        [ -z "$1" ] && echo "usage: trash <files>" && exit 1
        for f in "$@"; do
            if [ ! -e "$f" ]; then
                echo "not found: $f"
                continue
            fi
            fullpath=$(readlink -f "$f")
            name=$(basename "$f")
            # handle name collisions
            dest="$name"
            n=1
            while [ -e "$TRASH_FILES/$dest" ]; do
                dest="${name}.$n"
                n=$((n + 1))
            done
            mv "$f" "$TRASH_FILES/$dest"
            cat > "$TRASH_INFO/$dest.trashinfo" <<EOF
[Trash Info]
Path=$fullpath
DeletionDate=$(date '+%Y-%m-%dT%H:%M:%S')
EOF
            echo "trashed: $f"
        done
        ;;
esac