user2419571
user2419571

Reputation: 139

Unix: Bash:Searching for specific directories

I am working on a script in Bash and I am trying to find specific directories. I am very close, I am just stuck.

I am searching for directories that have one to three digits in their name.

Ex: Ex1, Ex23,Ex456.

When I fined these directories, I want to run a certain script.

I can get it to go through all single digit directories, but when I try to do get my script to do more than one digit, it does not work.

Edit: When I say it doesn't work, it will run and find a directory with 3 digits in it, but it ignores all directories with two or one digit.

Script:

for directory in  Ex[0-9][?0-9][?0-9]/
do
    ./test "$directory"
    echo "Directory is: " $directory
done

Upvotes: 0

Views: 54

Answers (3)

Sas
Sas

Reputation: 2503

Also you could do:

for directory in $(find . -name "Ex[0-9]*" | grep -E "Ex[0-9]{1,3}$")
do
   #My stuff
   ......
done

Upvotes: 0

LogicIO
LogicIO

Reputation: 623

This should work:

 for directory in Ex[0-9]*
    do 
       ./test "$directory"
       echo "Directory is: " $directory
    done

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 81032

The simplest solution is just to use three globs.

for directory in Ex[0-9] Ex[0-9][0-9] Ex[0-9][0-9][0-9]

You could use the =~ regex matching facilities to do the filtering in the loop instead if you wanted. Something like (untested):

for directory in  Ex[0-9]*/
do
    if [[ "$directory" =~ Ex[0-9]{1,3} ]]; then
        ./test "$directory"
        echo "Directory is: " $directory
    fi
done

Upvotes: 2

Related Questions