-
Notifications
You must be signed in to change notification settings - Fork 2
/
DelDir
executable file
·56 lines (46 loc) · 1.6 KB
/
DelDir
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
#!/usr/bin/env bash
. "${BASH_SOURCE[0]%/*}/function.sh" script || exit
usage()
{
ScriptUsage "$1" "\
Usage: $(ScriptName) [DIR]... [OPTION]...
Safely delete a directory
-a, --ask ask before deleting
-c, --contents only delete the contents of the directory, not the directory itself
-f, --files only delete the files in the directory
-H, --hidden delete hidden files or directories as well"
}
init() { defaultCommand="delete"; }
argStart() { unset -v ask contents files hidden; }
opt()
{
case "$1" in
--ask|-a) ask="true";;
--contents|-c) contents="true";;
--files|-f) files="true";;
--hidden|-H) hidden="true";;
*) return 1;;
esac
}
args() { ScriptArgGet "dir" -- "$@"; shift; }
deleteCommand()
{
[[ ! -d "$dir" ]] && return
dir="$(GetFullPath "$dir")" || return
local desc; [[ $contents ]] && desc=" the contents of"
if [[ $ask ]]; then
ask "Are you sure you want to delete$desc directory '$(FileToDesc "$dir")'" -dr n || return 1
fi
[[ $verbose ]] && printf "Deleting${desc} directory '$(FileToDesc "$dir")'..."
# delete directory
[[ ! $contents ]] && { RunLog2 rm -fr "$dir"; [[ $verbose ]] && echo "done"; return; }
# delete directory files and hidden
local args=(-type f,d) rmArgs=(-fr)
[[ $files ]] && args=(-type f) rmArgs=(-f)
[[ ! $hidden ]] && args+=(-not -name '.*')
[[ $test ]] && { [[ $verbose ]] && echo; ${G}find "$dir" -maxdepth 1 "${args[@]}" -not -path "$dir" | xargs echo rm "${rmArgs[@]}"; return; }
${G}find "$dir" -maxdepth 1 "${args[@]}" -not -path "$dir" -print0 | xargs -0 rm "${rmArgs[@]}" || return
[[ $verbose ]] && echo "done"
return 0
}
ScriptRun "$@"