Skip to content

Instantly share code, notes, and snippets.

@davydmaker
Created December 25, 2024 05:03
Show Gist options
  • Save davydmaker/be4204350fbd45e6c9c884c96021ab5a to your computer and use it in GitHub Desktop.
Save davydmaker/be4204350fbd45e6c9c884c96021ab5a to your computer and use it in GitHub Desktop.
Organize Files into Subdirectories in Batches
#!/bin/bash
FILES_PER_DIR=40
DIR_COUNT=1
FILE_COUNT=0
SCRIPT_NAME=$(basename "$0")
FILES=$(ls -1 | grep -v "^$SCRIPT_NAME$" | sort)
mkdir -p "$DIR_COUNT"
IFS=$'\n'
for FILE in $FILES; do
if [ -f "$FILE" ]; then
mv "$FILE" "$DIR_COUNT/"
FILE_COUNT=$((FILE_COUNT + 1))
if [ "$FILE_COUNT" -ge "$FILES_PER_DIR" ]; then
DIR_COUNT=$((DIR_COUNT + 1))
mkdir -p "$DIR_COUNT"
FILE_COUNT=0
fi
fi
done
echo "Files successfully distributed into subdirectories!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment