How to check if filename contains digits within an if condition

89 Views Asked by At

In the following script I match files consisting of numbers 0-9 in the pattern "Buchung_cdate_[0-9].pdf":

#!/bin/bash

dir="/home/benji/Nextcloud/Buchhaltung/test/"
tag="Buchung"
images="(*.png|*.jpg|*.jpeg|*.JPG|*.JPEG|*.webp)"
#convert="convert $f "Buchung_cdate.pdf""

#Preperations
shopt -s extglob

#Actual script starting here – do not change unless you know what you are doing ;)
for f in $dir*$images; do #loop on every file that has extension $images
    if [[ "$f" != *$tag* ]]; then #exclude files of the loop with filename including $tag
      cdate="$(date -r "$f" +"%Y-%"m"-%"d)"
      if [[ -f $dir"Buchung_$cdate.pdf" ]]; then #checks if "Buchung_$cdate.pdf" matches an already existing file in $dir
        if [ -f $dir"Buchung_$cdate"_[0-9].pdf ]; then
          echo $f" - Already existing!"
        else
          echo $f" - Already existing! Convert!"
        fi
      #echo $f" - Already existing!"
      else
      echo $f" - cdate not equal --> convert to \"Buchung_cdate.pdf\"!"
    fi
    else
      echo $f" - I include \"Buchung\" in my filename - Do nothing"
    fi
done

But what I really want is, to check if there is any number in place and raise this number by 1. (Like in the usecase when copying files a.pdf to a_1.pdf, a_3.pdf, … )

Any hints how to solve this?

See above. I already tried it with [0-9] but that is not what I am searching for.

0

There are 0 best solutions below