Re: OT: Bash script help needed

On Thu, 2007-12-20 at 08:30 -0500, Craig Puchta wrote:
This is off topic, but I'm not sure where to look for answers.

I am looking for some help in making a script to make directories and
move files to those directories. I am doing it manually and since I have
tens of thousands of these files to move, I figure there must be an
easier way to do it.
Here is what I have and want to do.

In one directory I have tens of thousands of .txt files named as such:

and so on.

What I want to do (and not manually, as I haven't even put a dent in it
yet) is:

mkdir rdb-001
mv rdb-001-* rdb-001
mkdir rbg-002
mv rbg-002-* rbg-002

I want to make a script that can grab a file name, make the directory
and move all corresponding files to that directory. The problem is I
don't know what command could grab a file name and make a directory from
Any help would be appreciated.

A little script like this would do what you want assuming the naming
conventions as above.

Go through it and make sure you understand what each part is doing and
edit it for your needs before you try running it.

If you need to get more complex with the base names and paths look
into 'sed' and 'awk' instead of 'cut'. is a great sed tut.

# Sort logs into folders.
# example log name "rdb-001-001.txt"


for i in `ls $WORKINGDIR`; do
basename=`echo $i | cut -c -7`
# test for an existing directory
if [ ! -d ${BASEPATH}/${basename} ]
# create the new dir if needed.
then mkdir -p ${BASEPATH}/${basename}

# now move the file(s)
mv ${WORKINGDIR}/${basename}* ${BASEPATH}/${basename}/


Let me know what you think.
This will need to be added to if your files aren't all in the same dir
at the moment or if there are spaces or strange characters in the names.


ubuntu-users mailing list
Modify settings or unsubscribe at: