Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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