Created
December 25, 2024 05:03
-
-
Save davydmaker/be4204350fbd45e6c9c884c96021ab5a to your computer and use it in GitHub Desktop.
Organize Files into Subdirectories in Batches
This file contains 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 | |
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