check if multiple folders exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if multiple folders exist
# 1  
Old 09-13-2011
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:
Code:
dirct_1
dirct_2
dirct_3
...

in the current directory. I did:
Code:
if [ -d dirct* ] 

then
echo " directories exist "

else 
echo " directories not exist "

fi

However, it returns me:
Code:
[: too many arguments

Then I tried to count the lines of ls output with:
Code:
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
Code:
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
# 2  
Old 09-13-2011
Code:
 
ls -d dir*/ && echo "Directory Available" || echo "Not available"

# 3  
Old 09-13-2011
DELETED

Last edited by yazu; 09-13-2011 at 08:02 AM..
# 4  
Old 09-13-2011
Use the find command,
Code:
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,
Code:
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
This User Gave Thanks to jgt For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

2. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

3. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

4. Shell Programming and Scripting

awk check if folder exist

Hello Im trying to build a script that (among other stuff) checks each line in a file for the $4 "variabel" and if there are a folder in the directory that has that not has the same name as that it create a folder, otherwise it wont do anything. I guess i could just send the errors to /dev/null,... (2 Replies)
Discussion started by: fisk4
2 Replies

5. Shell Programming and Scripting

Check if file exist

Hi, I created following script to check if file exist: #!/bin/bash SrcDir=$1 SrcFileName=$2 SrcTimePeriod=$3 if ;then echo 1 else echo 0 fi I ran it like: /apps/Scripts/FileExist.sh /apps/Inbox file1 2nd_period_2010 Even file exist at that location, my above command is... (4 Replies)
Discussion started by: palak08
4 Replies

6. Shell Programming and Scripting

Check if file exist

Hi, I am trying to create a bash script which will check if file exist then remove that file else do nothing. I have to do same process for three files in same script. I have written code for one file and trying to run it. if then rm -r /user1/abc/File1 fi When I run this code it... (1 Reply)
Discussion started by: palak08
1 Replies

7. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

8. Shell Programming and Scripting

Check if file exist

Hi Does anybody know how I can check if a file exists i.e. see bellow, this doesn't work by the way and if tried countless variations on this file1=$one/file111.txt if then echo "Present" else echo "Not present" fi result : Not present (file is already present, eventhough its... (3 Replies)
Discussion started by: gksenthilkumar
3 Replies

9. Shell Programming and Scripting

check the directory exist

I have the below script to check whether directory is exist or not , now I sure the directory /abc NOT exist , but when run the script , it still pop the result is "the directory exist" , could suggest what is wrong ? thx ll -d /abc > /dev/null 2>&1 if then echo "the directory exist !!" ... (7 Replies)
Discussion started by: ust
7 Replies

10. UNIX for Dummies Questions & Answers

how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it? i know if i can use find, but i would like to have a good interface (in a shell script) thks (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question