Linux – test filenames for regex patterns in bash

bashlinuxregex

I'm not sure exactly how the code should be written but I want to test a file/folder for naming patterns, something like:

if [ -d $i ] && [ regex([0-9].,$i) {
       do something
    }

I want it to check if the file/folder is a directory and that the name of it is a number (i.e. 1 or 101 or 10007)…

Best Answer

[ cannot do regular expressions. However, [[ can:

if [[ -d $i -a $i =~ ^[0-9]+$ ]] ; then
  ...
fi