-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyrm
160 lines (140 loc) · 3.89 KB
/
myrm
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# I write this bash shell for my careless wife who usually say Opp's after
# removing files;-)
#
# See documentation after the codes of this file for more info about version
# log installing and etc.
#
#
# -- setting up, modify if needed ----
#
# Definitions, enviromental variable TRASHCAN can override default value
DEFAULT_TRASHCAN_PATH=~/.trashcan.myrm
#
# Files to be removed go to one of the 'to be removed' directories, I call them
# "buffer". Image there is a queue before really removing.
# Define BUFFER_SIZE to control the queue's size, which offers times to rollback
# (un-delete).
#
BUFFER_SIZE=8
#
# system's default rm is suppose to be /bin
SYSTEM_RM=/bin/rm
#
# -- end settings --------------------
# use default trashcan direcotry if needed.
if [ -z "$TRASHCAN" ]; then
TRASHCAN="$DEFAULT_TRASHCAN_PATH"
fi
# test if the name for trashcan is occupated by other file
if [ -f "$TRASHCAN" ]; then
echo "Error: There is a file named with $TRASHCAN"
exit
fi
# create trash can directory if needed
if [ ! -d "$TRASHCAN" ]; then
mkdir "$TRASHCAN"
fi
# trash can disk space usage
if [ "$1" = "--diskusage" -o "$1" = "-d" ]; then
du -h -s "$TRASHCAN"
exit
fi
# purge trash can
if [ "$1" = "--purge" ]; then
$SYSTEM_RM -rf "$TRASHCAN"/*
exit
fi
# load pointer
POINTER=0
if [ -f "$TRASHCAN"/pointer ]; then
POINTER=`cat "$TRASHCAN"/pointer`
fi
# undo
if [ "$1" = "--undo" -o "$1" = "-u" ]; then
mv "$TRASHCAN"/$POINTER/* .
POINTER=$[$POINTER-1]
if [ $POINTER -le 0 ]; then
POINTER=$[$BUFFER_SIZE-1]
fi
echo $POINTER > "$TRASHCAN"/pointer
exit
fi
# show pointer only
if [ "$1" = "--showpointer" -o "$1" == "-s" ]; then
echo $POINTER
exit
fi
POINTER=$[$POINTER+1];
if [ $POINTER -ge $BUFFER_SIZE ]; then
POINTER=0
fi
echo $POINTER > "$TRASHCAN"/pointer
TARGETDIR="$TRASHCAN"/$POINTER
if [ -d "$TARGETDIR" ]; then
$SYSTEM_RM -rf "$TARGETDIR"/* # empty buffer first
else
mkdir "$TARGETDIR" # make current dir
fi
# by calling myrm without parameters, purge the oldest removed files
if [ -z "$1" ]; then
du -h -s "$TRASHCAN"
exit
fi
# ignore the rm options such as -rf
while [ -n "$1" ]
do
if [ "${1:0:1}" != "-" ]; then
mv -f "$1" "$TARGETDIR" # move file to current buffer
fi
shift
done
exit
# code end here, documentation follows
# -- Documents ---------------------
#
# Intruducing
#
# Program gives a brief protection by moving file to be removed to a pre-defined
# trash can directory and executing real removing laterly.
#
# Install
#
# Simply alias rm to this script to make it working for you.
# Don't forget to make your modifying takes effect by re-login.
#
# Update logs:
# Aug-23-2005: bug fixed for cygwin
# The solution is to cooperate with the $HOME directory containing spaces
# Sep-13-2003: long file name supported.
# Refer to:
# http://www.linuxforums.org/forum/topic-24094.html
#
# Sep-30-2003: support more options
# --diskusage, -d display disk usage by trash can
# --undo, -u recover the lastest removed files into current dir.
# --showpointer, -s show pointer for recover manully.
#
# Sep-24-2003: can filter off system rm's option such as -rf.
# support --diskusage and --purge options
#
# Sep-23-2003: First version released
#
# Help:
# How many disk space trash can is using?
# rm --diskusage
#
# Purge trash can
# rm --purge
#
# How to restory removed files?
# 1. goto your trash can directory, default is ~/.trashcan.myrm
# 2. check the sub-directories to see where are wanted files
# (file "pointer" will tell you which is the most recent
# recover directory)
# 3. simply move them back into your work directory.
# (another simpler way is using --undo option to recover the lastest removed
# files directly.)
#
# Author: Rui Luo, lplusplus@gmail
# Last Date: Sep-13, 2004