folderizer
This script moves large numbers of files into subfolders sorted alphabetically.
#!/bin/bash
# Create folders for letters and numbers
for letter in {A..Z}; do
mkdir -p "$letter"
done
for num in {1..9}; do
mkdir -p "$num"
done
# Create a folder for punctuation-based files
mkdir -p "M"
# Function to handle files starting with a punctuation mark
handle_punctuation() {
for file in *; do
if [[ -f $file ]]; then
first_char="${file:0:1}"
if [[ $first_char =~ [[:punct:]] ]]; then
new_name="_${file}"
mv "$file" "$new_name"
first_char="${new_name:0:1}"
if [[ $first_char =~ [[:digit:]] ]]; then
mv "$new_name" "$first_char/"
elif [[ $first_char =~ [A-Za-z] ]]; then
# Normalize to uppercase for letter folders
first_char=$(echo "$first_char" | tr '[:lower:]' '[:upper:]')
mv "$new_name" "$first_char/"
else
mv "$new_name" "M/"
fi
fi
fi
done
}
# Function to move files based on their starting letter
move_files_by_letter() {
for file in *; do
if [[ -f $file ]]; then
first_char="${file:0:1}"
if [[ $first_char =~ [A-Za-z] ]]; then
# Normalize to uppercase for letter folders
first_char=$(echo "$first_char" | tr '[:lower:]' '[:upper:]')
mkdir -p "$first_char"
mv "$file" "$first_char/"
elif [[ $first_char =~ [[:digit:]] ]]; then
mkdir -p "$first_char"
mv "$file" "$first_char/"
fi
fi
done
}
# Execute the functions
handle_punctuation
move_files_by_letter