Last active
February 28, 2018 10:24
-
-
Save tejovanthn/9125280843eb6634fb3c6a064a998e38 to your computer and use it in GitHub Desktop.
Bash script to split the current directory into N sub directories each with a set number of files.
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
# split_folder | |
# | |
# Usage: | |
# split_folder NumberOfFilesPerFolder FileType | |
# | |
# Example: | |
# split_folder 100 "*.jpg" | |
# split_folder 250 "*.NEF" | |
function split_folder { | |
#Optional Positional Arguments | |
N_FILES=${1:-100} # Number of files you want in each folder. | |
FILES=${2:-"*.*"} # Filetype, accepts any string. | |
let m=0 # Loop vairable. | |
let M=0 # Folder name variable. | |
for i in $FILES; do | |
let m=$m+1 | |
if [ $m -gt $N_FILES ]; then # Reset loop variable. Increment folder name. | |
let m=0 | |
let M=$M+1 | |
fi | |
if [ ! -d "$M" ]; then # Create the folder if it doesn't exist. | |
echo "Creating $M" | |
mkdir "$M" | |
fi | |
mv "$i" "$M" # Move this file. | |
done | |
echo "Done" # Done. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment