-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplode
More file actions
executable file
·203 lines (188 loc) · 6.16 KB
/
Copy pathexplode
File metadata and controls
executable file
·203 lines (188 loc) · 6.16 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# explode - Move a directory's contents up one level, then remove the empty directory
#
# Usage:
# explode [options] <directory>
#
# Options:
# -f, --force Overwrite existing files at the destination
# -n, --dry-run Show what would happen without making changes
# -v, --verbose List each item as it is moved
# -h, --help Display this help message
_explode() (
local SCRIPT_NAME; SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
case "${BASH_SOURCE[0]}" in /dev/*|/proc/*) SCRIPT_NAME="" ;; esac
case "$SCRIPT_NAME" in ""|bash|sh|zsh|dash) SCRIPT_NAME="explode" ;; esac
_show_help() {
local s; [ -t 1 ] && s=$'\033[4m'
local r; [ -t 1 ] && r=$'\033[24m'
cat <<EOF
NAME
$SCRIPT_NAME - move a directory's contents up one level, then remove it
SYNOPSIS
$SCRIPT_NAME [${s}options${r}] <${s}directory${r}>
$SCRIPT_NAME -h
DESCRIPTION
Moves every item in ${s}directory${r} into its parent, then removes the
now-empty ${s}directory${r}. Aborts without changes if any items would
collide with existing files, unless --force is given.
OPTIONS
-f, --force Overwrite existing files at the destination
-n, --dry-run Show what would happen without making changes
-v, --verbose List each item as it is moved
-h, --help Display this help message
EXIT STATUS
0 Success (or help/dry-run completed)
1 Runtime failure (a move failed, or directory removal failed)
2 Usage error, or precondition not met (bad args, missing
directory, collision without --force)
EOF
}
_error() { echo "[ERR][$SCRIPT_NAME] $*" >&2; }
_expand_short_opts() {
# $1 = string of short-opt letters that take a value (e.g. "nXHd"); "" for flag-only scripts
# $2..$N = "$@"
# Populates _EXPANDED; caller does: set -- "${_EXPANDED[@]}"; unset _EXPANDED
local value_opts="$1"; shift
_EXPANDED=()
local passthru=""
local arg
local rest
local c
for arg in "$@"; do
if [ -n "$passthru" ]; then _EXPANDED+=("$arg"); continue; fi
case "$arg" in
--) passthru=1; _EXPANDED+=("$arg") ;;
--*|-|"") _EXPANDED+=("$arg") ;;
-[a-zA-Z]?*)
rest="${arg#-}"
while [ -n "$rest" ]; do
c="${rest%"${rest#?}"}"; rest="${rest#?}"
_EXPANDED+=("-$c")
case "$value_opts" in *"$c"*)
[ -n "$rest" ] && _EXPANDED+=("$rest")
rest="" ;;
esac
done ;;
*) _EXPANDED+=("$arg") ;;
esac
done
}
local force=false
local dryrun=false
local verbose=false
local dir
_expand_short_opts "" "$@"
set -- "${_EXPANDED[@]}"; unset _EXPANDED
while [ $# -gt 0 ]; do
case "$1" in
-f|--force)
force=true
shift
;;
-n|--dry-run)
dryrun=true
shift
;;
-v|--verbose)
verbose=true
shift
;;
-h|--help)
_show_help
return 0
;;
--)
shift
[ $# -gt 0 ] && dir="$1"
break
;;
-*)
_error "Unknown argument '$1'. Run \`$SCRIPT_NAME -h\` for usage"
return 2
;;
*)
if [ -n "$dir" ]; then
_error "Multiple directories specified. Run \`$SCRIPT_NAME -h\` for usage"
return 2
fi
dir="$1"
shift
;;
esac
done
if [ -z "$dir" ]; then
_error "No directory specified. Run \`$SCRIPT_NAME -h\` for usage"
return 2
fi
if [ ! -e "$dir" ]; then
_error "$dir does not exist"
return 2
fi
if [ ! -d "$dir" ]; then
_error "$dir is not a directory"
return 2
fi
local dest; dest="$(dirname "$dir")"
# Check for collisions
# -d '' delimits on null byte, pairing with find -print0 for safe filename handling
local collisions=()
local item
while IFS= read -r -d '' item; do
local base; base="$(basename "$item")"
if [ -e "$dest/$base" ]; then
collisions+=("$base")
fi
done < <(find "$dir" -mindepth 1 -maxdepth 1 -print0)
if [ ${#collisions[@]} -gt 0 ] && ! $force; then
_error "Aborting -- the following items already exist in $dest:"
local c
for c in "${collisions[@]}"; do
echo " $c" >&2
done
return 2
fi
if $dryrun; then
if [ ${#collisions[@]} -gt 0 ]; then
echo "Would overwrite (--force):"
local c
for c in "${collisions[@]}"; do
echo " $c"
done
fi
echo "Would move to $dest:"
find "$dir" -mindepth 1 -maxdepth 1 -exec basename {} \; | sort
echo "Would remove: $dir"
return 0
fi
# Move contents
local mv_flags="-n"
$force && mv_flags="-f"
local failed=false
while IFS= read -r -d '' item; do
if $verbose; then
echo "$(basename "$item") -> $dest/"
fi
if ! mv "$mv_flags" "$item" "$dest/"; then
_error "Failed to move $item"
failed=true
fi
done < <(find "$dir" -mindepth 1 -maxdepth 1 -print0)
if $failed; then
_error "Some items failed to move. Directory not removed"
return 1
fi
# Remove the now-empty directory
if ! rmdir "$dir"; then
local num_remaining; num_remaining="$(find "$dir" -mindepth 1 -maxdepth 1 -print0 | tr -cd '\0' | wc -c | tr -d ' ')"
_error "Failed to remove $dir ($num_remaining items remaining)"
return 1
fi
)
_explode "$@"
__explode_rc=$?
unset -f _explode
if [ -n "${BASH_SOURCE[0]}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then
eval "unset __explode_rc; return $__explode_rc"
fi
eval "unset __explode_rc; exit $__explode_rc"