Skip to content

Commit

Permalink
Update sort_mkdir.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Feb 17, 2024
1 parent b813f04 commit 0029411
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions sort_mkdir.sh
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"

0 comments on commit 0029411

Please sign in to comment.