Shell script to move files to 3 different folders

Hi guys:

I've got this problem, I want to move a bunch of files to 3 different folders, without any specific order, and I'm trying to automatize it with a shell script.
I'm a newbie at shell scripting so this is my first try:

#!/bin/bash

COUNTER=`ls -1 | wc -l`

while [ "$COUNTER" != "0" ]
do
    ARRAY=( $(ls | head -n 3) ) 
    mv ${ARRAY[0]} folder1/
    mv ${ARRAY[1]} folder2/
    mv ${ARRAY[2]} folder3/
done

But I can't make it work. When executed it moves half of the files to folder1, the other half to folder2 and deletes folder3 (?). It also creates a folder called folder1 inside folder1, but it doesn't do that with folder2.
As you can see, I'm trying to do this by reading the first 3 files inside the folder where the script is executed and then putting them inside an array to make the 3 mv statements. I'm sure it's all messed up.
I'll really appreciate your help with this one.

try

#!/bin/bash
ls|head -3|while read line; do
cp "$line" "./folder1/"
cp "$line" "./folder2/"
cp "$line" "./folder3/"
rm "$line"
done

If the file is changing and you want to make sure all three copies are the same, move the file to the first location, then copy from the first location to the other two locations.

thanx a lot, i'm trying it right now

---------- Post updated at 03:05 PM ---------- Previous update was at 02:51 PM ----------

I just tryied it and it just copies first 3 files to all 3 folders and then deletes them from source folder, it's almost working as i need to, but it has to continue copying the rest of the files (could be 100 or 200 files in source folder), maybe i wasn't that clear, the script has to copy or move recursively all files from source folder in order to distribute them in the 3 folders, not move all files to all folders, i'm gonna try a mix between both scripts
thanx

---------- Post updated at 03:07 PM ---------- Previous update was at 03:05 PM ----------

I just tryied it and it just copies first 3 files to all 3 folders and then deletes them from source folder, it's almost working as i need to, but it has to continue copying the rest of the files (could be 100 or 200 files in source folder), maybe i wasn't that clear, the script has to copy or move recursively all files from source folder in order to distribute them in the 3 folders, not move all files to all folders, i'm gonna try a mix between both scripts
thanx

try cp -R instead of just cp

cp -R will copy subdirs also

why? That was not the question.

#!/bin/bash
find . -type f | while read file; do
    cp "$file" "./folder1/"
    cp "$file" "./folder2/"
    cp "$file" "./folder3/"
    rm "$file"
done

I think my script was working from the beginning but I was executing it inside the source folder so 'ls' command was listing directories too and moving them (at least the first one), I just executed it from outside source folder (with proper paths) and it ran OK. The only problem left is that doesn't seem to stop, maybe something about the counter, hope you can figure it out. I'll do more trials. Thanx.

while [ "$COUNTER" != "0" ]

it never reaches 0, so it has nothing left to do but a continuous loop of nothing, you need to break at somepoint

It should be in a for loop instead of while.

change

COUNTER=`ls -1 | wc -l`
while [ "$COUNTER" != "0" ]
to

COUNTER=`ls -1 | wc -l`
for (( $i=0; $i < "$COUNTER" ; i++ )); do

You're right, thanx.
However, this for loop is still missing the fact that variable COUNTER needs to decrease by 3 in every loop, or maybe add 3 to $i, What do you think?

for (( i=$COUNTER; $i > 0; )); do
echo $i
let i-=1
done

I hope this helps

I solved it!! Thanx for all sighK. This is the code for my script. It is used to move files (in my particular case were tiff images) from a folder to 3 different folders distributing the files among them. For example, if source folder has 100 files, the 3 target folders have to end up with 34, 33 and 33 files respectively. Hope this could be helpful to someone.

#!/bin/bash

COUNTER=`ls -1 *.TIF | wc -l`

while [ $COUNTER -gt 0 ]
do
        ARRAY=( $(ls *.TIF | head -n 3) )
        mv ${ARRAY[0]} folder1/
        mv ${ARRAY[1]} folder2/
        mv ${ARRAY[2]} folder3/
        COUNTER=`expr $COUNTER - 3`

done

You can change file extension or just delete it, folder paths and of course, the number of folders, just remember to change 'head -n' where n is the number of files to list first that will end up inside each folder. You also have to change the number in `expr $COUNTER - 3` to match the number of folders.

The following is a bit shorter and allows an arbitrary number of folders

#!/bin/bash
i=1
for f in *.TIF; do
	mv "$f" "folder$i/"
	((i=i%3+1))
done