-
Notifications
You must be signed in to change notification settings - Fork 69
/
split-into-folders.sh
executable file
·63 lines (53 loc) · 2.02 KB
/
split-into-folders.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
#!/usr/bin/env bash
set -eEuo pipefail
: "${FOLDER_PATTERN:="%05d000"}"
: "${START_NUMBER:=0}"
: "${OUTPUT_FOLDER:=$(pwd)}"
: "${FILES_PER_FOLDER:=1000}"
# shellcheck source=./lib.sh
. "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/lib.sh"
for arg in "$@"; do
case $arg in
-o=*|--output-folder=*) OUTPUT_FOLDER="${arg#*=}" ;;
-sn=*|--start-number=*) START_NUMBER="${arg#*=}" ;;
-fp=*|--folder-pattern=*) FOLDER_PATTERN="${arg#*=}" ;;
-fpf=*|--files-per-folder=*) FILES_PER_FOLDER="${arg#*=}" ;;
-*) handle_script_arg "$arg" ;;
*) break ;;
esac
shift # past argument=value or argument with no value
done
unset -v arg
if [[ "$#" == "0" ]]; then
echo "Please specify folders with files to split"
exit 1
fi
current_folder_num="$START_NUMBER"
find "$@" -type f ! -name "*.${OUTPUT_METADATA_EXTENSION}" | sort ${FILE_SORT_FLAGS[@]:+"${FILE_SORT_FLAGS[@]}"} | {
while true; do
chunk=$(cat_n "$FILES_PER_FOLDER")
numfiles=$(echo "$chunk" | wc -l)
decho "Found $numfiles number of files..."
if (( numfiles < FILES_PER_FOLDER )); then
decho "Insufficient for a new pack, breaking!"
break
fi
#shellcheck disable=SC2059
current_folder="$(printf "$FOLDER_PATTERN" "$current_folder_num")"
current_folder_num=$((current_folder_num+1))
decho "Creating folder '$OUTPUT_FOLDER/$current_folder' and '$OUTPUT_FOLDER/$current_folder.${OUTPUT_METADATA_EXTENSION}'..."
if [[ "$DRY_RUN" == "false" ]]; then
mkdir "$OUTPUT_FOLDER/$current_folder"
mkdir "$OUTPUT_FOLDER/$current_folder.${OUTPUT_METADATA_EXTENSION}"
fi
echo "$chunk" | while IFS= read -r file_to_move || [[ -n "$file_to_move" ]] ; do
decho "Moving file '$file_to_move' to '$OUTPUT_FOLDER/$current_folder/' and the meta file to '$OUTPUT_FOLDER/$current_folder.${OUTPUT_METADATA_EXTENSION}/'"
if [[ "$DRY_RUN" == "false" ]]; then
mv --no-clobber "$file_to_move" "$OUTPUT_FOLDER/$current_folder/"
mv --no-clobber \
"$file_to_move.${OUTPUT_METADATA_EXTENSION}" \
"$OUTPUT_FOLDER/$current_folder.${OUTPUT_METADATA_EXTENSION}/"
fi
done
done
}