You are not logged in.

#1 2009-09-01 16:59:54

nikihr
Member
From: Stockholm
Registered: 2009-09-01
Posts: 3
Website

[SOLVED] Need help with simple a script.

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

#2 2009-09-01 17:36:48

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: [SOLVED] Need help with simple a script.

#!/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

#3 2009-09-01 17:56:15

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Need help with simple a script.

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 smile.

Offline

#4 2009-09-01 18:05:33

klixon
Member
From: Nederland
Registered: 2007-01-17
Posts: 525

Re: [SOLVED] Need help with simple a script.

#!/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

#5 2009-09-01 18:12:35

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: [SOLVED] Need help with simple a script.

klixon wrote:
#!/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

#6 2009-09-01 18:16:50

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Need help with simple a script.

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

Offline

#7 2009-09-01 18:17:59

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: [SOLVED] Need help with simple a script.

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 smile.

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

#8 2009-09-01 18:18:37

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: [SOLVED] Need help with simple a script.

brisbin33 wrote:
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

#9 2009-09-01 18:22:16

nikihr
Member
From: Stockholm
Registered: 2009-09-01
Posts: 3
Website

Re: [SOLVED] Need help with simple a script.

Thank you so much guys, i will try some of this solutions as soon as i get home.

Offline

#10 2009-09-01 18:26:31

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Need help with simple a script.

scragar wrote:
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 smile.

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. tongue.

anyways, i think the OP's got more than he bargained for, hopefully we'll see a [SOLVED] on this one soon.

Offline

Board footer

Powered by FluxBB