in reply to sequencial file naming

First, I suggest that you call them 0001.dat, 0002.dat, so that do appear sorted when you use ls.

One way, would be to read the directory, sort the files, take the last one and increment it by one. This might become quite long if there are many files.

Another way would be to store the last number somewhere (DB, file) whatever. Then, just out of mere paranoia, you can can still check whether the proposed file exists, and increment the number as long as it does:

$file_n = do_something_to_get_the_number(); $file_n++ while -e "$file_n.dat";

It is likely that you'll call -e only once, if your stored number doesn't go out of sync.

--bwana147