-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipAllFolders.sh
69 lines (55 loc) · 1.38 KB
/
ZipAllFolders.sh
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
#!/bin/bash
[[ -n "$1" ]] && { cd "$1" || exit 1; }
# Get a yes/no choice from the user.
getchoice() { # Args: 1:prompt. Returns: 0:true 1:false
local choice
while true; do
read -n 1 -r -p "$1 [Y,N] " choice
echo
case $choice in
[Yy] ) return 0 ;;
[Nn] ) return 1 ;;
* ) echo "Invalid input." ;;
esac
done
}
# Get human-readable size from bytes.
prettysize() { # Args: 1:size
local size=$1
local unit="B"
local roundup=0
for u in KB MB GB TB; do
if [[ "$size" -ge 1024 ]]; then
unit="$u"
roundup=$((size & 512))
((size >>= 10))
else break
fi
done
[[ "$roundup" -gt 0 ]] && ((++size))
echo "$size $unit"
return 0
}
getchoice "Remove original folders?" && rmFolders="true"
echo
count=0
for f in */ ; do
folder="${f%/}"
[[ "$folder" = "*" ]] && break
[[ -L "$folder" ]] && continue
((++count))
echo "Folder $count: $folder"
zipname="${folder}.zip"
(cd "$folder" && zip "../$zipname" -r -0 . -i \\* > /dev/null || exit 1)
zipsize="$(wc -c < "$zipname")"
echo "Size ~ $(prettysize "$zipsize")"
if [[ "$rmFolders" = "true" ]]; then
rm -rf "$folder"
echo "Folder deleted."
fi
echo
done
echo "Done."
read -n 1 -r -s -p "Press any key to continue . . . "
echo
exit 0