Automatic name file with increase

Hello,

A file named c15a is registered on my disc all 10 minutes.

I must rename that file with the actually Date and with an automatic increase number on 5 digits, so as : c15a.20070528.00001 and the next file will be : c15a.20070528.00002.

The next day so tomorrow the five digits must be reset to 00001 so as for tomorrow : c15a.20070529.00001.

I have done the script with the rename with the date but i have a problem with the automatic increase number.

So can you help me with that increasing five digits number and automatic reset when the day changed.

So thanks a lot , bye.

You can try something like that :

filename=c15a

datestamp=$(date +%Y%m%d)
last_file=$(ls -1 $filename.$datestamp.* 2>/dev/null | tail -1)
if [ -z "$last_file" ]
then
  number=1
else
  number=$(( ${last_file##*.} + 1 ))
fi
new_filename=$(printf "%s.%s.%05.5d" "$filename" "$datestamp" "$number")

mv $filename $new_filename

Jean-Pierre.

Deleted the statement...

normally if used with tail -1, we would want ls -1tr instead. however for this particular case,i think because the filenames are already timestamped , with a number at the back, when doing ls -1, its still "sorted" to latest file.

Yes, indeed your are correct. Plz ignore my previous msg

Hello ,

thanks for your quick answer.

Just a question : the little below is in C coding , isn't it ?

new_filename=$(printf "%s.%s.%05.5d" "$filename" "$datestamp" "$number")

The same line in shell script can be better because i'm not a C developper.

Ok thanks a lot bye.

bash also has a printf ( think its from coreutils )...anyway, type man printf, or "info printf" to see if you have it.