-
Notifications
You must be signed in to change notification settings - Fork 0
/
unrm
executable file
·109 lines (89 loc) · 2.41 KB
/
unrm
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
#!/bin/bash
archivedir="$HOME/.deleted-files"
realrm="$(which rm)"
move="$(which mv)"
dest=$(pwd)
if [ ! -d "$archivedir" ]
then
echo "$(basename $0) fail - No delete directory, nothing to restore" >&2
exit 1
fi
cd $archivedir
if [ $# -eq 0 ]
then
echo "Contents of your deleted files archive (sorted by date):"
ls -FC | sed -e 's/\(\.[[:digit:]][[:digit:]]\)\{5\}//g' | sed -e 's/^/ /'
exit 0
fi
matches="$(ls -d "$1"* 2> /dev/null | wc -l)"
if [ $matches -eq 0 ]
then
echo "No matches found for \"$1\" in archive directory, nothing to restore" >&2
exit 1
fi
if [ $matches -gt 1 ]
then
echo "More than one file or directory match in the archive:"
index=1
for name in $(ls -td "$1"* )
do
datetime="$(echo $name | awk -F. '{ print $6"/"$5" at "$4":"$3":"$2 }')"
filename="$(echo $name | cut -d. -f1)"
if [ -d $name ]
then
filecount="$(ls $name | wc -l | sed 's/[^[:digit:]]//g' )"
echo "$index ) $filename (contents = ${filecount} items, deleted = $datetime)"
else
size="$(ls -sdk1 $name | awk '{print $1}')"
echo "$index ) $filename (size = ${size}Kb, deleted = $datetime)"
fi
index=$(( index + 1 ))
done
echo ""
/bin/echo -n "Which version of $1 should be restored ('0' to quit)?: "
read desired
if [ ! -z "$(echo $desired | sed 's/[[:digit:]]//g')" ]
then
echo "$(basename $0): Restore canceled by user: invalid input." >&2
exit 1
fi
if [ ${desired:=1} -ge $index ]
then
echo "$(basename $0): Restore canceled, index value too big" >&2
exit 1
fi
if [ $desired -lt 1 ]
then
echo "$(basename $0): Restore canceled, invalid index value" >&2
exit 1
fi
restore="$(ls -td1 "$1"* | sed -n "${desired}p")"
if [ -e "$dest/$1" ]
then
echo "$(basename $0): \"$1\" File already exists in the current path. Cannot overwrite." >&2
exit 1
fi
/bin/echo -n "Restoring file \"$1\" ..."
$move $restore "$dest/$1"
echo "done."
/bin/echo -n "Delete the additional copies of this file? [y] "
read answer
if [ "${answer:=y}" == 'y' ]
then
$realrm -rf "$1"*
echo "Deleted."
else
echo "Additional copies retained."
fi
else
if [ -e "$dest/$1" ]
then
echo "$(basename $0): \"$1\" File already exists in the current path. Cannot overwrite." >&2
exit 1
fi
restore="$(ls -d "$1"*)"
/bin/echo -n "Restoring file \"$1\" ..."
$move $restore "$dest/$1"
echo "done."
fi
exit 0