-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten
executable file
·150 lines (130 loc) · 3.3 KB
/
flatten
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
#!/bin/bash
set -eo pipefail
throw() {
printf '%s: %s\n' "${0##*/}" "$1" >&2
return 1
}
if [[ $# == 0 || $1 =~ ^(--help|-h)$ ]]; then
cat >&2 <<EOF
Usage: flatten [-p|--prefix <prefix>] [-s|--suffix <suffix>]
[-d|--delimiter <delimiter>] [-n|--dry-run] <directory>
Flattens the contents of a directory "dir" such that "dir/foo/bar/file.jpg" is
renamed to "dir/foo - bar - file.jpg" with the empty directories "bar" and
"foo" removed afterward. Existing files are never overwritten.
Options:
-p, --prefix <prefix> Optional string prepended to filenames.
-s, --suffix <suffix> Optional string appended to filenames (before
the file extension).
-d, --delimiter <delimiter> Separator added between path components.
Defaults to " - ".
-n, --dry-run Show how files would be renamed and exit.
EOF
exit 1
fi
paths=()
prefix=
suffix=
delimiter=' - '
dry_run=
while (( $# > 0 )); do
case $1 in
--)
shift
paths+=("$@")
break
;;
# prefix
-p|--prefix)
shift
prefix=$1
;;
-p*)
prefix=${1#-p}
;;
--prefix=*)
prefix=${1#--prefix=}
;;
# suffix
-s|--suffix)
shift
suffix=$1
;;
-s*)
suffix=${1#-s}
;;
--suffix=*)
suffix=${1#--suffix=}
;;
# delimiter
-d|--delimiter)
shift
delimiter=$1
;;
-d*)
delimiter=${1#-d}
;;
--delimiter=*)
delimiter=${1#--delimiter=}
;;
# dry run
-n|--dry-run)
dry_run=1
;;
-*)
throw "unknown option: $1"
;;
*)
paths+=("$1")
;;
esac
shift
done
if (( ${#paths[@]} == 0 )); then
throw 'no directory given'
elif (( ${#paths[@]} > 1 )); then
throw 'too many directories given'
elif [[ ! -d ${paths[0]} ]]; then
throw "not a directory: ${paths[0]}"
fi
directory=${paths[0]}
echo 'Scanning directory...'
file_list=$(cd "$directory" && find . -type f | sed 's/^.\///')
count_not_moved=0
while read -r orig_path; do
orig_path_without_ext=${orig_path%.*}
ext=
if [[ $orig_path == *.* ]]; then
ext=.${orig_path##*.}
fi
new_path="${prefix}${orig_path_without_ext//\//${delimiter}}${suffix}${ext}"
full_orig_path="$directory/$orig_path"
full_new_path="$directory/$new_path"
if [[ $full_orig_path == "$full_new_path" ]]; then
continue
fi
echo
if [[ -e $full_new_path ]]; then
printf '\e[31mAlready exists: %s\e[m\n' "$new_path"
(( count_not_moved += 1 ))
continue
fi
if [[ $dry_run ]]; then
printf 'Would rename \e[32m%s\e[m\n' "$orig_path"
printf ' to \e[32m%s\e[m\n' "$new_path"
continue
fi
printf 'Renaming \e[32m%s\e[m\n' "$orig_path"
printf ' to \e[32m%s\e[m\n' "$new_path"
mv -n "$full_orig_path" "$full_new_path" 2> >(sed $'s/.*/\e[31m&\e[m/')
done <<<"$file_list"
echo
if [[ ! $dry_run ]]; then
echo 'Removing empty directories...'
find "$directory" -mindepth 1 -depth -type d -empty -exec rmdir -- {} \;
fi
echo 'Done.'
if (( count_not_moved > 0 )); then
(( count_not_moved == 1 )) && plural= || plural=s
printf '\n\e[31m%s file%s skipped to avoid overwriting existing files.\e[m\n' "$count_not_moved" "$plural"
fi
exit $((count_not_moved > 255 ? 255 : count_not_moved))