You are not logged in.
Hello, Iam searching for a small script that sorts my folders in alphabetical order.
I want the script to create folder from A to Z and then move every folder that begins with A into the A folder etc etc..
I really hope anyone understands what im searching for here.
Last edited by nikihr (2009-09-01 20:25:33)
Offline
#!/bin/bash
for I in {a..z}; do ## for I in all the letters a-z
echo "Moving $I"; ## tell us what we are doing
mkdir $I; ## make the directory
echo "mv $I* \"$(echo $I | tr [:lower:] [:upper:])\"* $I/"; ## say the next command:
# mv $I* "$(echo $I | tr [:lower:] [:upper:])"* $I/ ## move the file
done
I'd run it like that first, check that it's not going to do anything damaging, then run it with the mv uncommented.
Last edited by scragar (2009-09-01 17:41:31)
Offline
be careful, mv $I* will pick up your freshly created directory too;
i'd `mkdir ./sorted/$I` instead, then `mv ./sorted/* ./ && rm -r ./sorted` at the end.
you could also use
find ./ -maxdepth 1 -iname "$I*" -exec mv {} ./sorted/ \;
to do the actual move*
*note: untested .
//github/
Offline
#!/bin/bash
LETTERDIRS=/location/of/letterdirs
for letter in a b c d e # i'm lazy so i'll stop right here
do
[[ -d ${LETTERDIRS}/${letter} ]] || mkdir -p ${LETTERDIRS}/${letter}
find . -type d -maxdepth 1 -iname $x\* -exec mv '{}' ${LETTERDIRS}/${letter} \;
done
make sure /path/to/letters is not in the directory containing the directories to be moved
EDIT: oooh... that {a..z} thingy is nice... better use that lol!
EDIT: internationalisation from dutch: s/letter/character/g
Last edited by klixon (2009-09-01 18:10:24)
Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!
Offline
#!/bin/bash LETTERDIRS=/location/of/letterdirs for letter in a b c d e # i'm lazy so i'll stop right here do [[ -d ${LETTERDIRS}/${letter} ]] || mkdir -p ${LETTERDIRS}/${letter} find . -type d -maxdepth 1 -iname $x\* -exec mv '{}' ${LETTERDIRS}/${letter} \; done
make sure /path/to/letters is not in the directory containing the directories to be moved
EDIT: oooh... that {a..z} thingy is nice... better use that lol!
You can put quotes over partial file paths to protect against space.
MYDIR="file path/with spaces";
echo "Creating $MYDIR";
mkdir -p "$MYDIR";
touch "$MYDIR/foo.bar";
echo "$MYDIR"/*
Also, you might want to use `tr` so the script works for both upper and lower case letters.
Last edited by scragar (2009-09-01 18:13:38)
Offline
be careful, mv $I* will pick up your freshly created directory too;
i'd `mkdir ./sorted/$I` instead, then `mv ./sorted/* ./ && rm -r ./sorted` at the end.
you could also use
find ./ -maxdepth 1 -iname "$I*" -exec mv {} ./sorted/ \;
to do the actual move*
*note: untested .
While my `mv` code will throw an error the error will not stop the moving of the files, you'll just get 26 messages about being unable to move the directory to itself. I could probably find a way to avoid this that doesn't require the use of find(which I personally dislike) or a second dir, probably just using a file prefix like "_" or something. It would only be a small edit:
#!/bin/bash
for I in {a..z}; do
echo "Moving $I";
mkdir "_$I";
echo "mv $I* \"$(echo $I | tr [:lower:] [:upper:])\"* _$I/";
# mv $I* "$(echo $I | tr [:lower:] [:upper:])"* "_$I/";
mv "_$I" $I
done
Offline
scragar wrote:Also, you might want to use `tr` so the script works for both upper and lower case letters.
man find, that's what -iname is for
Sorry, didn't notice the i, I read it as -name.
Offline
Thank you so much guys, i will try some of this solutions as soon as i get home.
Offline
brisbin33 wrote:be careful, mv $I* will pick up your freshly created directory too;
i'd `mkdir ./sorted/$I` instead, then `mv ./sorted/* ./ && rm -r ./sorted` at the end.
you could also use
find ./ -maxdepth 1 -iname "$I*" -exec mv {} ./sorted/ \;
to do the actual move*
*note: untested .
While my `mv` code will throw an error the error will not stop the moving of the files, you'll just get 26 messages about being unable to move the directory to itself. I could probably find a way to avoid this that doesn't require the use of find(which I personally dislike) or a second dir, probably just using a file prefix like "_" or something. It would only be a small edit:
#!/bin/bash for I in {a..z}; do echo "Moving $I"; mkdir "_$I"; echo "mv $I* \"$(echo $I | tr [:lower:] [:upper:])\"* _$I/"; # mv $I* "$(echo $I | tr [:lower:] [:upper:])"* "_$I/"; mv "_$I" $I done
well, i have to politely disagree: one, invoking find once is much better IMHO than mv with tr in a subshell. and two, my ./sorted/ idea adds one additional mv command and one additional rm command where you're _$I suggestion added 26 additional mv commands.... just sayin'
but we could also paint the bikeshed blue. .
anyways, i think the OP's got more than he bargained for, hopefully we'll see a [SOLVED] on this one soon.
//github/
Offline