Sequential numbering from 1 to ten

Hi I am in a bind, I need create a script that will rename files as they come into a folder with sequential numbering at the begining starting at 1 and proceeding to ten then starting at 1 again. Such as 1_filename.pdf, 2_filename.pdf, 3_filename.pdf, 4_filename.pdf, 5_filename.pdf, 6_filename.pdf, 7_filename.pdf, 8_filename.pdf, 9_filename.pdf, 10_filename.pdf it has to continue as long as new files are entering the folder. I was thinking of using a while loop to do this. but its late and my brains are shutting off.
Any help would be much appreciated

How about this:

#!/bin/bash

max_num() {
   max=0
   for file in [0-9][0-9][0-9]_*
   do
      [ ! -f "$file" ] && return
      num=${file%%_*}
      [ $num -gt $max ] && max=$num
   done
}

cd /path/to/folder
while true
do
    max_num
    for file in *
    do
       [[ ! -f $file || "$file" = [0-9][0-9][0-9]_* ]] && continue
       ((max++))
       printf -v new_name "%03d_%s" $max "$file"
       mv "$file" "$new_name"
    done
    sleep 1
done

---------- Post updated at 12:57 PM ---------- Previous update was at 12:15 PM ----------

Sorry just re-read your requirements and I think this is probably closer to what you asked for:

#!/bin/bash

mv_down() {
   for ((i=9; i ; i--)) {
       [ -f "${i}_$1" ] && mv -f "${i}_$1" "$((i+1))_$1"
   }
   mv "$1" "1_$1"
}

cd folder
while true
do
    for file in *
    do
       [[ ! -f "$file" || "$file" = [1-9]_* || "$file" = 10_* ]] && continue
       mv_down "$file"
    done
    sleep 1
done

Thanks very much again for your help, can I ask just one more thing? there is a small error in the script somewhere, it doesn't seem to want to count past 1_?
Thanks you very much again

Are you creating files with the same name?

eg:

[~]$ ./rename_files.bash &
[1] 8172
[~]$ cd folder
[~/folder]$ echo "Test 1" > paul
[~/folder]$ ls
1_paul
[~/folder]$ echo "Test 2" > paul
[~/folder]$ ls
1_paul  2_paul

Hi, the files actual have there own unique name, the script does work and count from 1 to 10 but only when the files names are identical to start with. Is there away to keep the original base name and still be able to to count from 1 to 10?
Thank very much for all your help

Try this and report back:

shopt -s extglob                # set bash's extglob option          
TMP=$(ls -t *[0-9]_* |head -1)
CNT=${TMP%%_*}                  # find maximum count till now
ls -tr !([1-9]_* 10_*) |        # apply extended pattern matching
        while read FN           # to exclude already renamed files
          do echo mv $FN $((1+CNT++%10))_$FN    # use modulo function
          done 

This version shuffles the existing files down and places new file at #1 (deleting old #10).

#!/bin/bash

mv_down() {
   rm -f 10_*
   for ((i=9; i ; i--)) {
       printf -v FILE "%s" ${i}_*
       [ -f "$FILE" ] || continue
       BASE=${FILE#${i}_}
       mv -f "$FILE" "$((i+1))_$BASE"
   }
   mv "$1" "1_$1"
}

cd folder
while true
do
    for file in *
    do
       [[ ! -f "$file" || "$file" = [1-9]_* || "$file" = 10_* ]] && continue
       mv_down "$file"
    done
    sleep 1
done