safe-rm

声明alias rm='safe-rm.sh'

主要用于放置rm误删除操作. 建立隐藏文件夹[按日期建立]用于保存删除文件

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
#!/bin/bash
# 安全的rm脚本


dir=$(date "+%y_%m_%d")
#dir="/Users/rocky/.usertrash/$dir"
dir="/Users/rocky/.Trash/$dir"
#echo $dir
if [ ! -d $dir ];then
mkdir -p $dir
fi


is_f=false
args=""


function f_remove() {
for i in ${args}; do
if [ -d "$i" -o -f "$i" ];then
name=`basename $i`
if [ -d "$dir/$name" -o -f "$dir/$name" ];then
new_name="$dir/${name}_$(date '+%T')"
mv $i $new_name && echo "$i deleted,you can see in $new_name"
else
mv $i $dir && echo "$i deleted,you can see in $dir/$i"
fi
else
echo "参数错误"
fi
done
}


function remove() {
for j in ${args}; do
if [ -d "$j" -o -f "$j" ];then
name=`basename $j`
#read -p "Remove $name?[y/n]" bool ## 是否开启删除提醒功能;l
#if [ $bool == "n" ];then
# exit
#elif [ $bool == "y" ];then
if [ -d "$dir/$name" -o -f "$dir/$name" ];then
new_name="$dir/${name}_$(date '+%T')"
mv $j $new_name && echo "$j deleted,you can see in $new_name"
else
mv $j $dir && echo "$j deleted,you can see in $dir/$j"
fi
#fi
else
echo "参数错误"
fi
done
}


while [ "$1" ]; do
case "$1" in
-fr|-rf)
is_f=true
shift
;;
-i)
is_f=false
shift
;;
*)
args="$1 $args"
shift
;;
esac
done


if [[ $is_f = true ]];then
f_remove
else
remove
fi
欣赏此文? 求鼓励,求支持!