-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b813f04
commit 0029411
Showing
1 changed file
with
28 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,38 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Check if a directory argument is provided | ||
if [ -z "$1" ]; then | ||
echo "Usage: $0 <directory>" | ||
exit 1 | ||
fi | ||
|
||
# Save the current working directory | ||
original_dir=$(pwd) | ||
|
||
# Change to the target directory | ||
cd "$1" || exit | ||
|
||
# Loop through numbers and letters as file name prefixes | ||
for letter in {0..9} {a..z}; do | ||
# Convert the letter to uppercase for the directory name | ||
upper_letter=$(echo "$letter" | tr '[:lower:]' '[:upper:]') | ||
|
||
# Initialize an empty array to store the file paths | ||
files_array=() | ||
upper_letter=$(echo "$letter" | awk '{ print toupper($0) }') | ||
if find "$1" -maxdepth 1 -type f -iname "$letter*" -print0 | grep -q .; then | ||
mkdir -p "${1}${upper_letter}" | ||
|
||
# Find files starting with the current prefix (case-insensitive) and populate the array | ||
if find . -maxdepth 1 -type f -iname "${letter}*" -print0 | grep -qz .; then | ||
mkdir -p "${upper_letter}" | ||
while IFS= read -r -d $'\0' file; do | ||
files_array+=("$file") | ||
done < <(find "$1" -maxdepth 1 -type f -iname "$letter*" -print0) | ||
done < <(find . -maxdepth 1 -type f -iname "${letter}*" -print0) | ||
|
||
# Move each file to the corresponding uppercase directory | ||
for file in "${files_array[@]}"; do | ||
mv "$file" "${1}${upper_letter}" | ||
mv "$file" "${upper_letter}/" | ||
done | ||
fi | ||
done | ||
|
||
# Return to the original directory | ||
cd "$original_dir" |