Shell回收站

以下两个脚本分来自当年整理并发表在chinaunix.net的论坛贴。这里补充一下为什么Linux没有回收站功能 !
来自《UNIX编程艺术》一书可以寻找到答案:

因为UNINX/Linux设计之初主要用于更专业的场景,从设计哲学上 ”就像你拿一把枪向自己开枪也是可以的“,因为他是为更专业人使用的一个系统。

《UNIX编程艺术》

2008年版本

以下是自己上学时写的,代码很简单,但是功能都是实现了,第一次在这发帖没什么写的就上学时写的这拿出来晒晒。
以前用shell写过一个rm但是并没有恢复功能,前一阵cu论坛上有个网友说能不能增加一个功能就是把删除的文件的原路径记录到一个文件中,所以我又写了这个rm增加了恢复功能,同时也能看到删除文件的原来路径。

rm功能:

  • rm -r
  • rm -f
  • rm -rf
  • rm -i
  • rm –help

以上功能和平时用的rm命令功能基本一样,”rm -l”是新增加的功能,它会把所有删除的文件显示一个列表,前边有行号你想恢复哪个文件只要按相应的行号就行,并按y确认。

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#-----------------------------------------------------------------
# filename:
# author: wds
# begin:2008.1.27
# end:2008.2.1
# version: v.2
# script address: http://blog.chinaunix.net/u1/40306/index.html
#-----------------------------------------------------------------
from1=$1
from2=$2
garbage=$HOME/.garbage
mvlog=$garbage/mv.log
if [ ! -e $garbage ]
then
mkdir -p $garbage
chmod 777 $garbage
fi
function rand
{
a=(0 1 2 3 4 5 6 7 8 9 a b c d e A B C D E F )
for ((i=0;i<7;i++));do
echo -n ${a[$RANDOM % ${#a[*]}]}
done
}
random=$(rand)
function rm1
{
if [ -d "$from1" ]
then
echo "rm: cannot remove '$from1/' : Is a directory"
else
echo "`pwd`/:$from1:$random:`date`" >> $mvlog
mv "$from1" "$garbage/$from1:$random"
fi
}
function more
{
for file in *
do
echo "`pwd`/:$file:$random:`date`" >> $mvlog
mv $file "$garbage/$file:$random"
done 2> /dev/null
}
function rmi
{
if [ ! -d "$from2" ]
then
echo -n "rm:remove regular empty file '$from2'?" ; read answer;
if [ "$answer" = 'y' -o "$answer" = 'Y' ]
then
echo "`pwd`/:$from2:$random:`date`" >> $mvlog
mv "$from2" "$garbage/$from2:$random"
fi
else
echo "rm: cannot remove directory '$from2': Is a directory"
fi

}
function rmf
{
if [ ! -d "$from2" ]
then
echo "`pwd`/:$from2:$random:`date`" >> $mvlog
mv "$from2" "$garbage/$from2:$random"
else
echo "rm: cannot remove directory '$from2': Is a directory"
fi
}
function rmr
{
if [ -e "$from2" ]
then
result=$(echo $from2 | sed 's/\///g')
echo "`pwd`/:$result:$random:`date`" >> $mvlog
mv "$result" "$garbage/$result:$random"
fi
}
function rml
{
while :
do
clear
line=$(cat -n $mvlog | awk -F : '{print $1,"FileName:"$2, "Time:"$4}')
linecount=$(cat $mvlog | wc -l)
echo -e "$line\c"
echo
echo
echo "Please input number you want revent(line count:$linecount)--exit(e)"
read answer
if [ "$answer" = e -o "$answer" = E ]
then
break
else
(
echo "please input y(sure:)"
read answer1
if [ "$answer1" = y -o "$answer" = Y ]
then
address=$(sed -n "$answer""p" $mvlog | awk -F : '{print $1}')
filename=$(sed -n "$answer""p" $mvlog | awk -F : '{print $2}')
filerand=$(sed -n "$answer""p" $mvlog | awk -F : '{print $3}')
fullname=$address$filename
if [ -e "$fullname" ]
then
echo "The file exist!"
sleep 1
else
old="$garbage/$filename:$filerand"
new="$address$filename"
mv "$old" "$new"
delline=$( cat $mvlog | sed "$answer""d" | sort -o $mvlog)
echo "update ok!!!"
sleep 1
fi
fi
)
fi
done
}
function help
{
echo "
-i) If you wants delete some file , this function is confirm you want,the same as old rm.
-f) If you wants delete some directory ,you can use this function ,the same as old rm.
-r) If you wants delete some directory of file ,this function can use , the same as old rm.
-l) This is new function,is you wants resume some file or directory you can use this function,
first this function can list some file in you garbage , these have some number ,if you
wants resume 1,you can input 1 and then input y to confirm.
If you want add some function or some new idear please contact me...
author:wds
email:7717060@sina.com

"
}
case "$1"
in
[a-z]) : ;;
[0-9]) : ;;
[A-Z]) : ;;
?) more ;;
*) :;;
esac
if [ "$#" -eq 0 ]
then
echo -n "rm: missing operand
Try 'rm --help' for more informaction.
"
fi
if [ "$#" -eq 1 ]
then
case "$from1"
in
-i) echo "Try 'rm --help' for more informaction."; break ;;
-f) echo "Try 'rm --help' for more informaction."; break ;;
-r) echo "Try 'rm --help' for more informaction."; break ;;

-l) rml ;;
--help) help;;
*) rm1;;
esac
fi

if [ "$#" -eq 2 ]
then
case "$from1"
in
-i) rmi ;;
-f) rmf ;;
-r) rmr ;;
-l) rml ;;
-rf) rmr ;;
--help) help ;;
esac
fi
if [ "$#" -gt 2 ]
then
for file in $*
do
mv $file "$home/"
done 2> /dev/null
fi

2007年版本

Shell回收站,当此目录到一定的大小后发邮件通知管理员。
命令安装方法:

  • cd /bin/ ; mv rm rm vm
  • cd /bin/ ; vi rm 增加以下内容

命令使用方法:

  • rm *
  • rm -r
  • rm -i
  • rm -rf 等,
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
#!/bin/bash
#wds
#v5.0
#2007.11.18
#7717060.cublog.cn
to=$1
to1=$2
home=$HOME/.garbage
dtime=$(date +%M-%H:%M)
userpid=$$
size=$(du -sh $home|awk '{printf $1}')
size1=$(echo $size | grep M)
size2=$(echo $size | sed 's/M//g')
ipaddr=$(/sbin/ifconfig eth0 | awk '/inet /{print substr($2,6)}')
adminemail=7717060@163.com
if [ -n "$size1" -a "$size2" -gt 100 ]
then
echo "$home is $size" | mail -s '$ipaddr' '$adminemail'
fi
if [ ! -e "$home" ]
then
mkdir -p $home
fi
myrmi ()
{
echo -e "rm: remove directory '$to1'?\c"
read -r answer
if [ "$answer" = y ]
then
if [ -d "$to1" ]
then
echo "rm: remove idrectory '$to1'?"
else
if [ -e "$home/$to" ]
then
mv "$to1" "$home/$to1-$userpid-$dtime"
else
mv "$to1" "$home"
fi
fi
fi
}
myrmrf ()
{
if [ -e "$to1" ]
then
mv "$to1" "$home/$to1-$userpid-$dtime"
else
mv "$to1" "$home/$to1-$userpid-$dtime"
fi
}
myrm ()
{
if [ -d "$to" ]
then
echo "rm cannot remove '$to': IS a directory"
else
if [ -e "$home/$to" ]
then
mv "$to" "$home/$to-$userpid-$dtime"
else
mv "$to" "$home"
fi
fi
}
if [ "$#" -eq 1 ]
then
case "$to"
in
-i) echo "Try 'rm --help' for more informaction." ;;
-rf) echo "Try 'rm --help' for more informaction." ;;
-r) echo "Try 'rm --help' for more informaction'" ;;
*) myrm;;
esac
fi
if [ "$#" -eq 2 ]
then
case "$to"
in
-i) myrmi ;;
-rf) myrmrf ;;
-r) myrmrf ;;
*) echo "'rm --help' for more informaction."
esac
fi
if [ "$#" -gt 2 ]
then
for file in $*
do
mv $file "$home/"
done 2> /dev/null
fi