-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrt.for_China
92 lines (84 loc) · 3.02 KB
/
rt.for_China
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
#!/bin/bash
## rt/remove_trash.sh
## 1. It is designed to replace old command rm without having to delete it.
## 2. It can avoid deleting files or directories with old command rm by mistake.
## 3. You can continue to use new 'rm' to delete files or directories, and it will move the target file or directory to
## a trash directory instead of directly deleting them.
## 4. Besides, you can set the time to automatically empty the trash directory.
## https://github.com/Velcon-Zheng/rm-rt
## Author: WeigangZheng, [email protected]
## Date: 2020-04-05
##******First step******
## git clone https://github.com/Velcon-Zheng/rm-rt.git
## cp rm-rt/rt ~/ ##cp rm-rt/rt to your directory you want;
## echo 'alias "rm= ~/rt"' >> ~/.bashrc ; source ~/.bashrc;
##******Second step******
## crontab -e
## 0 0 * * 0 rm -rf ~/zwg.trash/*
#########################
alias rm="~/rt" ## Replace rm command with rt command, the "~/rt" should be set to your global path. It should be written in ~/.bashrc
Trashdir=~/zwg.trash ## Set to your trash directory, and suggest not set to your home ~/
## Check the order is correct
if [ ! -d $Trashdir ]; then
echo -e "Warning: $Trashdir not exist;\nTry to creat $Trashdir"
mkdir $Trashdir || exit ;
fi
if [ $# -lt 1 ]; then
echo "Error: no target for rm"
echo "Usage: rm myfile mydirectory"
exit
fi
AA="del"
AA2="rec"
BB=`basename $1`
CC=1
DD=1
onoff="No"
Time=`date | sed 's/ \+/-/g'`
## move the target files or directories to a trash directory or permanently delete them
for i in $*; do
if [ $CC -lt 2 ]
then
if [ "$AA" == "$BB" ]
then
DD=$[DD+1]
read -t 10 -p "请确认是否永久删除文件或文件夹? Y/N:" onoff
#read -t 10 -p "Are you sure to permanently delete files or directories? Y/N:" onoff
echo -e "\n"
if [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" )
then
unalias rm
fi
elif [ "$AA2" == "$BB" ]
then
echo -e "正在恢复文件或文件夹 ...... \n"
else
TrashFileName=`basename $i`
mv $i $Trashdir/$TrashFileName-$Time
fi
else
if [ $DD -ge 2 ] && ( [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" ) )
then
rm -rif $i
elif [ "$AA2" == "$BB" ]
then
mv $Trashdir/*$i* ./
else
TrashFileName=`basename $i`
mv $i $Trashdir/$TrashFileName-$Time
fi
fi
CC=$[CC+1]
done
if [ $DD -ge 2 ] && ( [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" ) )
then
echo -e "警告: 已经直接将文件或文件夹删除 \n"
#echo -e "Warning: have permanently deleted the target file or directory \n"
elif [ "$AA2" == "$BB" ]
then
echo -e "完成: 已经恢复文件或文件夹 \n"
#echo -e "Finished: have resscue the target file or directory from $Trashdir \n"
else
echo -e "完成: 将文件或文件夹移至垃圾回收站 $Trashdir \n"
#echo -e "Finished: have moved the target file or directory to $Trashdir \n"
fi