-
Notifications
You must be signed in to change notification settings - Fork 1
/
saferm
66 lines (64 loc) · 1.94 KB
/
saferm
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
#!/bin/bash
# Check if it includes deleting the root directory
cannot_delete_dir=false
contains_root_dir=false
for ((i = 2; i <= $#; i++)); do
rp=$(realpath -s ${!i})
if (
[ "$rp" = "/bin" ] ||
[ "$rp" = "/boot" ] ||
[ "$rp" = "/data" ] ||
[ "$rp" = "/dev" ] ||
[ "$rp" = "/etc" ] ||
[ "$rp" = "/home" ] ||
[ "$rp" = "/lib" ] ||
[ "$rp" = "/lib32" ] ||
[ "$rp" = "/lib64" ] ||
[ "$rp" = "/libx32" ] ||
[ "$rp" = "/mnt" ] ||
[ "$rp" = "/media" ] ||
[ "$rp" = "/opt" ] ||
[ "$rp" = "/proc" ] ||
[ "$rp" = "/root" ] ||
[ "$rp" = "/run" ] ||
[ "$rp" = "/sbin" ] ||
[ "$rp" = "/snap" ] ||
[ "$rp" = "/srv" ] ||
[ "$rp" = "/sys" ] ||
[ "$rp" = "/tmp" ] ||
[ "$rp" = "/usr" ] ||
[ "$rp" = "/usr/bin" ] ||
[ "$rp" = "/usr/lib" ] ||
[ "$rp" = "/usr/lib32" ] ||
[ "$rp" = "/usr/libx32" ] ||
[ "$rp" = "/usr/sbin" ] ||
[ "$rp" = "/usr/local" ] ||
[ "$rp" = "/var" ]
); then
cannot_delete_dir=true
elif [[ $rp =~ ^/[^/]*$ ]]; then
contains_root_dir=true
fi
done
# Check if command-line arguments are "rm -rf /" or "rm -rf /*"
if [ "$#" -ge 2 ] && [ "$0" = "/usr/local/bin/saferm" ] && [ "$1" = "-rf" ]; then
if [ "$2" = "/" ] || [ "$cannot_delete_dir" = "true" ]; then
# Cannot delete specified directories
echo "Deletion of these directories is not allowed."
elif [ "$contains_root_dir" = "true" ]; then
# Prompt the user for confirmation
echo "Danger: Are you sure you want to delete the folders in this root directory? (yes/no): "
read confirmation
# Check the user's input
if [ "$confirmation" == "yes" ]; then
# If the user enters "yes," execute the system's rm command
/bin/rm "$@"
echo "Deletion successful."
else
# If the user enters anything else or "no," cancel the deletion operation
echo "Deletion canceled."
fi
else
/bin/rm "$@"
fi
fi