Last active
August 29, 2015 14:15
-
-
Save dbernheisel/53942b680198ab18c007 to your computer and use it in GitHub Desktop.
Bash script to move files into batched folders for easier processing. For example if there were 50k files and you needed them in 200 file chunks, you would call this to move the 50k into folders of 200 files each.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Usage: batch-folders.sh /Where/To/Look 5000 | |
| # This will create 5000-large batch folders within /Where/To/Look | |
| FolderPath="$1" | |
| HowManyToBatch="$2" | |
| BatchFolder="Batch_" | |
| # check if number of arguments is right | |
| if [ ! $# == 2 ]; then | |
| echo "Usage: $0 folder_path number_of_files_per_batch" | |
| exit 1 | |
| fi | |
| # check if folder exists | |
| if [ ! -d "$FolderPath" ]; then | |
| echo "Error: Path does not exist: $FolderPath" | |
| exit 1 | |
| fi | |
| # check if batch is a real number | |
| re='^[0-9]+$' | |
| if ! [[ $HowManyToBatch =~ $re ]] ; then | |
| echo "Error: Not a number: $HowManyToBatch" | |
| exit 1 | |
| fi | |
| # figure out how many files there are | |
| echo "Counting the files to divide..." | |
| NumberToDivide=$(find "$FolderPath" -type f -d 1 \( ! -iname ".*" \) | wc -l) | |
| echo "There are $NumberToDivide files to batch" | |
| # figure out how many batches will be needed for the number of files | |
| temp=$(expr $NumberToDivide + $HowManyToBatch - 1) | |
| batches=$(expr $temp / $HowManyToBatch ) | |
| echo "Calculated that $batches batches will be needed." | |
| echo "$NumberToDivide / $HowManyToBatch = $batches batches" | |
| # prepare batch folders | |
| for i in $(seq 1 $batches); do | |
| mkdir "$FolderPath"/$BatchFolder$i | |
| done | |
| # start moving content into batch folders | |
| batch=1 | |
| count=1 | |
| for f in $(find "$FolderPath" -type f -d 1 \( ! -iname ".*" \)); do | |
| if [ "$count" -gt "$HowManyToBatch" ]; then | |
| count=1 | |
| batch=$(expr $batch + 1) | |
| if [ "$batch" -gt "$batches" ]; then | |
| echo "Uh oh, I was about to move a file into an uncreated folder" | |
| exit 1 | |
| fi | |
| fi | |
| echo "Starting Batch $batch" | |
| file=$(basename "$f") | |
| mv -v "$f" "$FolderPath"/$BatchFolder$batch/"$file" | |
| count=$(expr $count + 1) | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment