Skip to main content
fix for when prefix and/or suffix have the same value as ID
Source Link
Peter.O
  • 33.8k
  • 32
  • 120
  • 167

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 

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 idpatt="$idpatt" ' length>=l{l=length;id=$2} END{print id}' ) [[ -z $idhigh ]] && echo "$pfix$idbase$sfix" || perl -E '$x="'$idhigh'"; $x++; print "'$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 

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 'length>=l{ l=length;  id=substr($0,'${#pfix}'+1,length-'${#pfix}-${#sfix}') }  END{ print id }' ) [[ -z $idhigh ]] && echo "$pfix$idbase$sfix" || perl -E '$x="'$idhigh'"; $x++; print "'${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 
avoided using temp var to hold list, and got rid of `wc`
Source Link
Peter.O
  • 33.8k
  • 32
  • 120
  • 167

This will give yououtputs the next sequential filename. The ID is based on a-z -> aa-az -> ba-bz ..can be any length and it can be either numeric or alphabetic. etcThis sample is primed to use an alpha ID, etc.the first ID being a

pfix='file_'; pfix='file_' sfix='.txt'; shopt -stxt' extglob ls="$(ls -1 "$pfix"+([a-z])"$sfix" 2>/dev/null)"idbase=a # 1st alpha #id listwhen matchingno files [[ -zexist $ls- ]]use &&a {decimal printfnumber "${pfix}a${sfix}";for exit;numeric }id's  idpatt='[a-z]' # alpha glob pattern - #use no'[0-9]' matchingfor filesnumeric foundid's maxlen=$(echoshopt "$ls"-s |extglob idhigh=$( wcls -L1 "$pfix"+($idpatt)"$sfix" 2>/dev/null |  awk -v FS="$pfix|$sfix" -v idpatt="$idpatt" '  # filter longest name(s)  sorting list alonelength>=l{l=length;id=$2} isEND{print notid}' enough) hiid="$(echo[[ "$ls"-z |$idhigh sed]] -n&& "/.\{$maxlen\}/{echo s/$pfix\|$sfix//gp}""$pfix$idbase$sfix" |||  tail -n1)"  perl -E '$x = "'$hiid'";'$x="'$idhigh'"; $x++; print "'$pfix'$x'$sfix'\n"' # increment the highest found ID 

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 

This will give you the next sequential filename. The ID is based on a-z -> aa-az -> ba-bz ... etc, etc.

pfix='file_'; sfix='.txt'; shopt -s extglob ls="$(ls -1 "$pfix"+([a-z])"$sfix" 2>/dev/null)" # list matching files [[ -z $ls ]] && { printf "${pfix}a${sfix}"; exit; } # no matching files found maxlen=$(echo "$ls" | wc -L) # filter longest name(s)  sorting list alone is not enough hiid="$(echo "$ls" | sed -n "/.\{$maxlen\}/{ s/$pfix\|$sfix//gp}" | tail -n1)" perl -E '$x = "'$hiid'"; $x++; print "'$pfix'$x'$sfix'\n"' # increment the highest found ID 

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 

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 idpatt="$idpatt" '  length>=l{l=length;id=$2} END{print id}' ) [[ -z $idhigh ]] && echo "$pfix$idbase$sfix" ||   perl -E '$x="'$idhigh'"; $x++; print "'$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 
output full filename (vs just ID) – use variables for prefix and suffix (vs hard coded)
Source Link
Peter.O
  • 33.8k
  • 32
  • 120
  • 167

This will give you the next sequential filename. The ID, is based on a-z ..-> aa-az ..-> ba-bz  ... etc, etc. – It assumes file_ is the basename's prefix, and .txt is the suffix

pfix='file_'; sfix='.txt'; shopt -s extglob ls="$(ls -1 file_+"$pfix"+([a-z]).txt"$sfix" 2>/dev/null)"  # list matching files [[ -z $ls ]] && { printf a;"${pfix}a${sfix}"; exit; }  # no matching files found longbnam=$maxlen=$(echo "$ls" | wc -L) # to  # filter longest name(s) – sortedsorting list alone is not enough hiid="$(echo "$ls" | sed -n '"/.\{'$longbnam'\$maxlen\}/{ s/file_\|\.txt$pfix\|$sfix//gp}'" | tail -n1)" perl -E '$x = "'$hiid'"; $x++; print "$x"'  "'$pfix'$x'$sfix'\n"' # increment the highest found ID 

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 

This will give you the next sequential ID, based on a-z .. aa-az .. ba-bz  ... etc, etc. – It assumes file_ is the basename's prefix, and .txt is the suffix

shopt -s extglob ls="$(ls -1 file_+([a-z]).txt 2>/dev/null)" # list matching files [[ -z $ls ]] && { printf a; exit; }  # no matching files found longbnam=$(echo "$ls" | wc -L) # to filter longest name(s) – sorted list alone is not enough hiid="$(echo "$ls" | sed -n '/.\{'$longbnam'\}/{ s/file_\|\.txt//gp}' | tail -n1)" perl -E '$x = "'$hiid'"; $x++; print "$x"'  # increment the highest found ID 

This will give you the next sequential filename. The ID is based on a-z -> aa-az -> ba-bz ... etc, etc.

pfix='file_'; sfix='.txt'; shopt -s extglob ls="$(ls -1 "$pfix"+([a-z])"$sfix" 2>/dev/null)"  # list matching files [[ -z $ls ]] && { printf "${pfix}a${sfix}"; exit; } # no matching files found maxlen=$(echo "$ls" | wc -L)   # filter longest name(s) – sorting list alone is not enough hiid="$(echo "$ls" | sed -n "/.\{$maxlen\}/{ s/$pfix\|$sfix//gp}" | tail -n1)" perl -E '$x = "'$hiid'"; $x++; print "'$pfix'$x'$sfix'\n"' # increment the highest found ID 

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 
cater for no matching files – add comments
Source Link
Peter.O
  • 33.8k
  • 32
  • 120
  • 167
Loading
Source Link
Peter.O
  • 33.8k
  • 32
  • 120
  • 167
Loading