How do I create sequentially numbered file names in bash?

today=$( date +%Y%m%d )   # or: printf -v today '%(%Y%m%d)T' -1
number=0

fname=$today.txt

while [ -e "$fname" ]; do
    printf -v fname '%s-%02d.txt' "$today" "$(( ++number ))"
done

printf 'Will use "%s" as filename\n' "$fname"
touch "$fname"

today gets today's date, and we initialise our counter, number, to zero and create the initial filename as the date with a .txt suffix.

Then we test to see if the filename already exists. If it does, increment the counter and create a new filename using printf. Repeat until we no longer have a filename collision.

The format string for the printf, %s-%02d.txt, means "a string followed by a literal dash followed by a zero-filled two-digit integer and the string .txt". The string and the integer is given as further arguments to printf.

The -v fname puts the output of printf into the variable fname.

The touch is just there for testing.

This will generate filenames like

20170125.txt
20170125-01.txt
20170125-02.txt
20170125-03.txt

etc. on subsequent runs.


You can use seq. It can create number sequences in variety of ways, however you need to know total number of files.

E.g: You can try seq -w 1 10. It will create the sequence from 01 to 10, then you can include it in a for loop:

for i in `seq -w 1 10`
do
  touch `date +%Y%m%d`-$i.txt
done

Addendum for your latest question update:

To accomplish what you want easily, you can create the first file with -0. On subsequent runs, you need to take the list of files, sort them, take the last one, cut it from last - and get the number, increment it and create the new file with that number.

Padding will need some more work though.


Something like...

#!/bin/bash
DATE=$(date +%Y%m%d)
filename="${DATE}.txt"
num=0
while [ -f $filename ]; do
    num=$(( $num + 1 ))
    filename="${DATE}-${num}.txt"
done
touch $filename

...should work. This creates filenames of the format DATE-1.txt, DATE-2.txt, DATE-3.txt, ..., DATE-10.txt, DATE-11.txt, etc. Changing that to DATE-01.txt etc is left as an exercise to the reader :)

Note that you should probably also make sure you don't call the script more than once concurrently, otherwise you'll have more than one script modifying things.

Side note: there is loads of software for managing multiple versions of a file. They're called "version control systems" (VCS), or "Source Control Management" (SCM). Git and subversion are pretty popular. I suggest you check them out, rather than reimplementing your own :-)