-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmexcept
74 lines (53 loc) · 1006 Bytes
/
rmexcept
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
#!/bin/bash
set -eu
SCRIPT_DIR=$(cd $(dirname $0); pwd)
source "${SCRIPT_DIR}"/abstract/common
function help(){
cat <<EOF
rmexcept
remove everything except specified files
this do not remove hidden files
usage:
+ rmexcept [option] <file>
+--- remove everything except <file>
option usage:
-h, -help, --help | display this help
-r, -R, --recursive | remove directories and their contents recursively
EOF
}
option=
while [ "$#" -gt 0 ]
do
case "$1" in
-r | -R | --recursive)
option="-r"
shift 1
;;
-h | -help | --help)
# TODO
# help
exit
;;
-*)
error "unknown option: '$1'"
;;
*)
break
;;
esac
done
notDeleteFiles=("$@")
for i in `seq 0 "$((${#notDeleteFiles[@]} - 1))"`
do
f="${notDeleteFiles[i]}"
mv "$f" ".$f"
done
# If set -e is used, rm * may fail, so do not specify it.
set +e
rm ${option} *
set -e
for i in `seq 0 "$((${#notDeleteFiles[@]} - 1))"`
do
f="${notDeleteFiles[i]}"
mv ".$f" "$f"
done