-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive
More file actions
executable file
·284 lines (225 loc) · 5.91 KB
/
Copy patharchive
File metadata and controls
executable file
·284 lines (225 loc) · 5.91 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# Script to automate cleaning and archiving of old work directories with lots of files
export LZIP_OPT="-9"
COMPRESS="tar --create --lzip --remove-files --file"
EXT="tar.lz"
INTERMEDIATE_EXT=( o ps out swp class tmp )
REDUNDANT_EXT=( git svn tar.gz tar.bz2 zip )
SOURCES_EXT=( doc docx ppt pptx odt odp )
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
MAGENTA="\033[0;35m"
CYAN="\033[0;36m"
BOLD="\033[1m"
RESET="\033[0m"
# Disables pathname expansion, so that we can run commands like find -name *.ext
# without expanding to matches in the local directory
set -f
function header() {
echo -e "${CYAN}${BOLD} >>> $@${RESET}"
}
function cmd() {
if pretend; then
echo -e " Would have run: ${BOLD}$@${RESET}" >&2
else
"$@"
fi
}
function file_match() {
local color="$1" file="$2" msg="$3"
printf " ${color}%20s > %s${RESET}\n" "$msg" "$file"
}
function file_delete() {
file_match "$RED" "$1" "${@:2}"
clean && cmd rm -r "$1" # Use '-r' to be able to remove directories
}
function file_force() {
file_match "$YELLOW" "$1" "${@:2}"
clean && force && cmd rm -r "$1"
}
function file_keep() {
file_match "$GREEN" "$1" "${@:2}"
}
function file_other() {
file_match "$MAGENTA" "$1" "${@:2}"
}
function error() {
echo -e "${RED}$@${RESET}"
}
function find_ext() {
local cmd="find . -name *.${1}"
for ext in "${@:2}"; do
cmd="$cmd -o -name *.${ext}"
done
echo "$cmd"
}
function intermediate() {
header "Searching for intermediate files..."
$(find_ext ${INTERMEDIATE_EXT[@]}) | while read f; do
file_delete "$f"
done
}
function redundant() {
header "Searching for redundant files..."
$(find_ext ${REDUNDANT_EXT[@]}) | while read f; do
for ext in ${REDUNDANT_EXT[@]}; do
local dir="${f%.$ext}"
if [[ "$f" != "$dir" ]];then
if [ -d "$dir" ]; then
file_delete "$f" "dir found"
else
file_force "$f" "no dir found"
fi
break
fi
done
done
}
function office() {
header "Searching for office files..."
if ! type loffice &>/dev/null; then
echo "loffice not found. Libreoffice needs to be installed to convert files to pdf."
return
fi
$(find_ext ${SOURCES_EXT[@]}) | while read original; do
local pdf="${original%.*}.pdf"
if [[ ! -f "$pdf" ]]; then
if clean; then
cmd loffice --convert-to pdf --outdir "$(dirname "$pdf")" "$original"
if ! pretend && ! [ -f "$pdf" ]; then
error "Could no generate pdf for '$original'"
continue
fi
fi
file_force "$original" "generate pdf"
else
file_force "$original" "pdf found"
fi
done
}
function duplicates() {
header "Searching for duplicate files (fdupes)..."
local first=""
fdupes --recurse --noempty . | while read -r f; do
if [[ ! "$f" ]]; then
first=""
continue
fi
if [[ ! "$first" ]]; then
file_keep "$f" "first copy"
first="$f"
else
file_other "$f" "duplicate"
clean && cmd ln --force "$first" "$f"
fi
done
}
function builds() {
header "Searching for build scripts..."
find . -name Makefile | while read f; do
grep "clean:" "$f" >/dev/null || continue
local dir=$(dirname $f)
file_other "$f" "make clean"
clean && cmd make --directory "$dir" clean
done
find . -name build.xml | while read f; do
file_other "$f" "ant clean"
clean && cmd ant -noinput -buildfile "$f" clean
done
}
function empty() {
header "Searching for empty files..."
find . -empty | while read f; do
file_delete "$f"
done
}
function run() {
if [[ $# < 1 ]]; then
for target in ${TARGETS[@]}; do
$target
done
elif [[ "$EXCLUDE" ]]; then
for target in ${TARGETS[@]}; do
if [[ ! "${@}" =~ $target ]]; then
$target
fi
done
else
for target in ${@}; do
if type -t $target &>/dev/null; then
$target
else
echo "Oops, unknown target '$target'."
exit 1
fi
done
fi
}
function compress() {
if [[ $# == 1 ]]; then
${COMPRESS} "${1}.${EXT}" "$1"
else
target=$1
shift
${COMPRESS} "${target}.${EXT}" "$@"
fi
}
function extract() {
tar --extract --file "$1" && rm "$1"
}
function check() { [[ "$CMD" == "check" ]]; }
function clean() { [[ "$CMD" == "clean" ]]; }
function pretend() { [[ "$PRETEND" == 1 ]]; }
function exclude() { [[ "$EXCLUDE" == 1 ]]; }
function force() { [[ "$FORCE" == 1 ]]; }
if [[ $# < 1 ]]; then
cat<<EOF
Usage:
$(basename $0) [<options>] <action> [<arguments>]
Options:
--pretend Show commands instead of running them
--exclude Targets given are to be treated as an exclusion list
--force Delete also uncertain unedeed files
Actions:
check [<targets>] Show matched files, with no actual deletion or other actions taking place
clean [<targets>] Delete and run other cleaning actions
compress <directory> Compress directory into directory.tar.lz, removing directory
compress <name> <files> Compress given files into name.tar.lz
extract <tar file> Extract given tar file
Targets:
builds Run 'make clean' for each Makefile found
empty Delete empty files or directories
intermediate Delete temporary stuff like .o files
duplicates Turn duplicate files into hard links
office Convert office files (like .doc or .xls) into pdf files
redundant Delete archive files (like .zip or .tar.gz) and repositories data (.git)
Output description:
Matched files are shown in a color depending on what action will be performed:
- $(echo -e ${GREEN}Kept intact${RESET})
- $(echo -e ${YELLOW}Will only be delete with --force${RESET})
- $(echo -e ${MAGENTA}Kept but somewhat changed${RESET})
- $(echo -e ${RED}Always deleted${RESET})
EOF
exit
fi
TARGETS=( builds intermediate redundant duplicates office empty )
while :; do
case $1 in
--pretend) PRETEND=1 ;;
--exclude) EXCLUDE=1 ;;
--force) FORCE=1 ;;
--*) echo "Unknown option '$1'" ;;
*) break
esac
shift
done
CMD="$1"
ARGS=( "${@:2}" )
case $CMD in
check) run "${ARGS[@]}" ;;
clean) run "${ARGS[@]}" ;;
extract) extract "${ARGS[@]}" ;;
compress) compress "${ARGS[@]}" ;;
*) echo "Unknown action '$CMD'" ;;
esac