Other How to sort images in to folders?

I would like to sort my images in to folders based on the first letter of the filename [0-9, a-z]. The folder is getting quite large and out of hand.

Is there any easy way to do this with a bash script?
 
This task is basically possible. The most difficult part might be to be sure what you exactly want to achieve. For the tasks themselves are many tools available. If you do not know them up to now it needs some time to get comfortable with them. The web provides a lot of information related to shell programming. The usenet group comp.unix.shell is worth to monitor as well. I am not sure if this answer matches your expectations. What do you think?
 
Dear scottro,
In other words, google it? One suspects the OP knew that and was looking for pointers.
I am sorry if my post was impolite or misleading. I just like to know about the knowledge the OP already has. It is just clear the OP is aware about the power of shell scripts. It might be that he has already used find, sort, sed and so on from time to time. Then it is not difficult to do the step to shell scripts. But it can also be the other way arround. From my perspective there is no clear indication in the initial question. Lets us wait for the answer of Mayhem30.
 
Fair enough, and I was actually trying to be droll rather than rude, though looking at my post again, it seems ruder than it sounded. Adding a smiley now would be after the fact, but it probably would have been appropriate, and I do see your point--find out the OP's base knowledge before giving explanations that may be self-standing instructions to you but not to them.
 
How many times will this be done? If it is a one-time thing, it can be done manually:
mkdir a-files b-files ...
mv a* a-files/
mv b* b-files/

And so on. If it will be used repeatedly, a script can be created.
 
Even if it's used once, something like below would be less tedious. Note, you'll get warnings that you can ignore, because it will try to move the newly created folder to itself.

for i in {a..z}; do mkdir $i && mv $i* $i; done

I'll leave it as an exercise to deal with files starting with numbers. I can't steal all the fun. I did this in zsh, but I think it will work in bash as well.
 
Not in sh(1), though. Not surprising, sh(1) is trapped in 1973. expr(1) can be used to get the first character of a string. I actually wrote a script to do this (it's good practice), but in general, it's a waste of time to program one-time operations.
 
Sure, it's a waste of time to write a script for one-time use, unless it's for fun and practice, but this is a one-line command. There is no need to worry about portability. It's also much less of a waste of time to write one command versus 27.
 
Even if it's used once, something like below would be less tedious. Note, you'll get warnings that you can ignore, because it will try to move the newly created folder to itself.

for i in {a..z}; do mkdir $i && mv $i* $i; done

I'll leave it as an exercise to deal with files starting with numbers. I can't steal all the fun. I did this in zsh, but I think it will work in bash as well.

Using jot(1) you can use this in any shell, for instance with /bin/sh

for i in `jot -c 26 a`; do mkdir $i && mv $i?* $i; done
 
Nice. I tested it out.

Code:
## generate some (100) random files in the format @Mayhem30 described
~/tmp % for i in `seq 1 100`; do RAND=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | head -c 5); touch ${RAND}.png; done
## command to solve @Mayhem30's question without warnings (@jalla used the mv target $i?*)
~/tmp % for i in `jot -c 26 a; jot 10 0`; do mkdir $i && mv $i?* $i; done

ADDED: jalla: s/any shell/any shell with Bourne syntax/
 
Because I do not trust shell scripts anywhere without extensive quoting, my effort was like this:
Code:
#!/bin/sh
targetdir="/tmp"

for fname in *; do
  firstchar=`expr "${fname}" : '\(.\)'`

  echo "mkdir -p "{$targetdir}/${firstchar}-files""
  echo "cp "${fname}" "${targetdir}/${firstchar}-files""
done

Note the echos to verify that it was doing the right thing, and cp rather than mv because one bad mv will ruin your day.
 
Back
Top