Script Sharing

rmanly
Contributor III

I posted this to our internal Tech wiki. I made some slight revisions and
posted for scripting lessons. I look at EVERY post on here that has anything
to do with scripting/CLI looking for new tips or tricks and so I am sharing.
;)

For various reasons I needed to throw around several hundred folders that
were loosely grouped into 6 folders. To make things simpler I wanted to
arrange them into 26, one for each letter.

Moving them from:

./A-D/Alice/
./A-D/Alex/
./A-D/Bob/
./A-D/Belinda/

to:

./A/Alice/
./A/Alex/
./B/Bob/
./B/Belinda/

etc.

so the first thing I did was:

$ mkdir {A..Z}

#NOTE I discovered that my shell of choice (zsh) will expand numbers like
bash {1..9} but not letters - this may be a setting I have tweaked or a
difference in bash compatibility

but then how to move everything quickly and easily? I came up with the
following. The way it is written currently it must be executed from the
directory that contains the lettered directories but this can easily be
changed.

#!/bin/bash

for letter in {A..Z}; do find . -iname "$letter*" -type f -print0 | xargs -0 -I {} mv {} $letter/
done

exit 0

Ryan M. Manly
Glenbrook High Schools

1 REPLY 1

rmanly
Contributor III

... -type d ...

For directories ;)