This outputs the next sequential filename. The ID can be any length and it can be either numeric or alphabetic. This sample is primed to use an alpha ID, the first ID being a
pfix='file_' sfix='.txt' idbase=a # 1st alpha id when no files exist - use a decimal number for numeric id's idpatt='[a-z]' # alpha glob pattern - use '[0-9]' for numeric id's shopt -s extglob idhigh=$( ls -1 "$pfix"+($idpatt)"$sfix" 2>/dev/null | awk -v FS="$pfix|$sfix" -v'length>=l{ idpatt="$idpatt"l=length; ' length>=l id=substr($0,'${l=length;id=$2#pfix}'+1,length-'${#pfix}-${#sfix}') } END{ print id }' ) [[ -z $idhigh ]] && echo "$pfix$idbase$sfix" || perl -E '$x="'$idhigh'"; $x++; print "'$pfix'$x'$sfix'\n"'"'${pfix}'"."$x"."'${sfix}'\n"' If no matching file exists, the output is:
file_a.txt If the highest matching file is file_zzz.txt, the output is:
file_aaaa.txt