check if multiple folders exist

I want to check if some directories with common prefix exist under current directory with bash, say, I have dictories like:

dirct_1
dirct_2
dirct_3
...

in the current directory. I did:

if [ -d dirct* ] 

then
echo " directories exist "

else 
echo " directories not exist "

fi

However, it returns me:

[: too many arguments

Then I tried to count the lines of ls output with:

DIR=$(ls ./dirct*> /dev/null | wc -l)
if [ "$DIR" != "0" ]
...
else
...

However, as I do not have the /dev/null in my ubuntu 10.04 system. I can not send the line

ls: cannot access ./dirct*: No such file or directory

to the null. So if there is no dirct* directory existing, there will be a trouble.

So, How could I avoid these problem?

Many Thanks!

Cristalp

 
ls -d dir*/ && echo "Directory Available" || echo "Not available"

DELETED

Use the find command,

find ./ -type d -name "dir*" -print
if  [ $? -eq 0 ]
then 
echo found

There is a problem with this if there are directories within directories.
Use grep,

ls -l |grep "^d" |grep dirct

works ok as long as you don't have a user or group named dirct..
You could also replace /dev/null with /tmp/$$.null or $HOME/$$.null

1 Like