-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip-all.sh
More file actions
executable file
·35 lines (29 loc) · 1.01 KB
/
zip-all.sh
File metadata and controls
executable file
·35 lines (29 loc) · 1.01 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
#!/bin/bash
# (C) 2025 Benjamin Steenkamer
# Zip uncompressed files in a directory into their own ZIPs
# Each file will be deleted after being successfully zipped
exit_script() {
exit 130
}
trap 'exit_script' SIGINT # Captures ctrl+c to cleanly exit for-loop
if [[ -z "$1" || ! -d "$1" ]]; then
echo "Usage: $0 <directory>"
exit 1
fi
IGNORE_ARCHIVES="(zip|rar|7z|gz|tgz|xz|bz2)$" # Don't compressed already compressed files
cd "$1"
for file in *; do
if [[ -f "$file" ]]; then
if [[ "$file" =~ $IGNORE_ARCHIVES ]]; then
echo "Already compressed: '$file'"
continue
fi
archive_name="${file%.*}.zip" # Remove file name, append zip
if [[ -f "$archive_name" ]]; then
echo "ERROR, ARCHIVE ALREADY EXISTS: '$archive_name'"
continue
fi
echo "Compressing '$file' to '$archive_name'"
7z a -bso0 -tzip -mm=Deflate64 -mx=9 -mmt=on -sdel -y "$archive_name" "$file" # bso0 = Only show errors and progress
fi
done