-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash
executable file
·112 lines (97 loc) · 2.86 KB
/
trash
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# trash
# A script similar to the recycle bin feature on windows platforms.
# It moves files/directorys to a defined directory instead of removing them from the filesystem.
# If you really want to get rid of them, you can use the trash-empty command.
# Version: 0.42
# Build: 2022-12-12
# Author: Heiko Barth
# Licence: Beer-Ware (See: https://en.wikipedia.org/wiki/Beerware)
APP=${0##*/}
VER=0.42
set -ueo pipefail
if [ -z "${TRASHDIR:-}" ]; then
echo 'Error: $TRASHDIR is not set.'
echo
exit 1
fi >&2
[ $# -eq 0 ] && {
echo "$APP $VER"
echo
echo "A script similar to the recycle bin feature on windows platforms."
echo "It moves files/directorys to a defined directory instead of removing them from the filesystem."
echo "If you really want to get rid of them, you can use the trash-empty command."
echo
echo "Syntax: $APP <directory/file> [<another directory/file>]"
echo
echo "Syntax: $APP-empty [options]"
echo
echo "Empty trash: $APP-empty"
echo "Shred some file types and empty trash: $APP-empty txt jpg"
echo "Shred all files and empty trash: $APP-empty --shred"
exit 1
} >&2
checkBinarys() {
local i
for i in "$@"; do
hash "$i" 2>/dev/null || {
echo "Error: Binary '$i' is missing."
echo
exit 1
} >&2
done
}
checkBinarys "basename" "date" "mkdir" "mv" "stat"
# shellcheck disable=SC2174
mkdir -p -m 700 "$TRASHDIR" 2> /dev/null || {
echo "Error: Failed to create directory '$TRASHDIR'."
echo
exit 1
} >&2
# Set your desired date format; See 'man date' for details
TODAY=$(date +%Y-%m-%d)
TRASHTODAY="$TRASHDIR/$TODAY"
# Create target directory
# shellcheck disable=SC2174
mkdir -p -m 700 "$TRASHTODAY" 2> /dev/null || {
echo "Error: Failed to create directory '$TRASHTODAY'."
echo
exit 1
} >&2
FAIL=0
for i in "$@"; do
# Abort if file does not exist and is no symbolic link (there can be symbolic links pointing to files that does not exist)
if [ ! -e "$i" ] && [ "$(stat -c %F "$i" 2>/dev/null)" != "symbolic link" ]; then
echo "Error: File/Directory '$i' does not exist." >&2
FAIL=1
continue
fi
# basename of file or directory
# ${i##*/} returns empty string for "directory/"
NAME=$(basename "$i")
# If target exist, find an alternate name
MAX=500
if [ -e "$TRASHTODAY/$NAME" ] || [ -h "$TRASHTODAY/$NAME" ]; then
j=0
while :; do
((++j))
if [ ! -e "$TRASHTODAY/$NAME.$j" ] && [ ! -h "$TRASHTODAY/$NAME.$j" ]; then
NAME="$NAME.$j"
break
fi
if ((j == MAX)); then
echo "Warning: '$TRASHTODAY/$NAME' already exists."
echo "Error: Could not find an alternative target name. Gave up, after $MAX iterations: $TRASHTODAY/$NAME.$j"
echo "Error: File '$i' not moved."
FAIL=1
continue
fi
done
fi
mv "$i" "$TRASHTODAY/$NAME" || {
echo "Error: Failed to move '$i' --> '$TRASHTODAY/$NAME.$j'"
FAIL=1
} >&2
done
[ $FAIL -eq 1 ] && echo
exit $FAIL