Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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)...

share|improve this question

2 Answers 2

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

if [[ -d $i -a $i =~ ^[0-9]+$ ]] ; then
share|improve this answer

If you're using Bash earlier than version 3.2, the regex match operator =~ either doesn't exist, works differently or has bugs. You can use this glob expansion form in that case (or if you're using a POSIX shell that doesn't have it).

case $i in
        if [ -d "$i" ]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.