franklin52 script is very good but needs a tweak to change the directory to lowercase.
tr "[A-Z]" "a-z"
BTW. Don't use this syntax because it is upset by single character filenames!
tr [A-Z] [a-z]
If you have a very large number of files (or the filenames contain space characters) the script will need some work to generate the list of files in a pipe without decending the directory tree. Depends on which unix and which version of "find" you have.
I know he already has his answer . . . but I see a lot of answers on here
that are overly complicated. The ksh itself has much of the functionality
of cut, awk and sed. For example:
# This creates a variable that is only 1 character-wide, left-justified,upper-case
typeset -u -L1 first_letter
cd images/main
/bin/ls *.jpg |
while read file ; do
first_letter=$file
mkdir $first_letter
/bin/mv $file $first_letter
done
This script has the added feature of being a single process.
Therefore, if it mattered, which it probably doesn't nowadays,
it would be the fastest way performance-wise to do this.
Worked great the 1st time, but now when I try add more files to the directory and re-run it just hangs. Is there an issue because the directory ($dir) has already been created? If so, how do I modify to check first to see if $dir exists?
It appears the original script worked the 1st time, but when I ran it again it created another subdirectory below the previous one. I tried running several times before I looked at the output and now I have a huge mess ...