-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrash
executable file
·99 lines (90 loc) · 1.8 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
#!/bin/sh
# trash: move files to trash
# this file in public domain
trashdir="${TRASH:-"${XDG_DATA_HOME:-"$HOME/.local/share"}/Trash"}"
retval=0
IFS="/"
usage() {
echo "usage: trash file..." >&2
exit 1
}
# make trash directory and set its mode
mktrashdir() {
if ! mkdir -p -- "$trashdir/files" "$trashdir/info" >/dev/null 2>&1
then
echo "trash: unable to create $trashdir"
exit 1
fi
chmod 700 "$trashdir"
}
# canonicalize "//", "/./", and "/../" from $filename into $path
setpath() {
path=""
case "$filename" in
([!/]*) filename="$PWD/$filename" ;;
esac
for segment in $filename
do
case "$segment" in
("") ;;
(".") ;;
("..") path="${path%/*}" ;;
(*) path="$path/$segment" ;;
esac
done
}
# break basename of $path into body and extension; set $newfile
# to "body.ext" (or "body_N.ext", for a unique file)
setnewfile() {
basename="${path##*/}"
body="${basename%.*}"
ext="${basename##"$body"}"
i=""
p=""
while test -e "$trashdir/files/$body$p$i$ext"
do
p="_"
if [ -z "$i" ]
then
i=1
else
i=$((i + 1))
fi
done
newfile="$body$p$i$ext"
}
# make $trashdir/info/$newfile.trashinfo file containing the original
# path of the trashed file and its date of deletion
mktrashinfo() {
cat > "$trashdir/info/$newfile.trashinfo" <<-END
[Trash Info]
Path=$path
DeletionDate=$(date +"%FT%H:%M:%S")
END
}
# move $path into $trashdir/files/$newfile
movetotrash() {
if ! mv -- "$path" "$trashdir/files/$newfile" >/dev/null 2>&1
then
echo "trash: $arg: could not move file to trash" >&2
retval=1
else
mktrashinfo
fi
}
[ $# -eq 0 ] && usage
mktrashdir
for arg
do
filename="$arg"
setpath
if test ! -e "$path"
then
echo "trash: $arg: no such file or directory" >&2
retval=1
else
setnewfile
movetotrash
fi
done
exit $retval