how to check existance of multiple directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to check existance of multiple directories
# 1  
Old 09-14-2011
Question how to check existance of multiple directories

Hi,

I would like to check whether all the directories exists or not. I tried the below but it gives some error. below is the excerpt from my original script
Code:
     24 #Check if the required directories are exists
     25 dirExists() {
     26
     27 if [ ! -d $SWDIR && ! -d $SWDIR/eaijava && ! -d $ipetoolsdir && ! -d $hawkdir ]
     28 then
     29 echo "required directories does not exists.. exiting.."
     30 exit
     31
     32 else
     33 echo "directories are exists.."
     34
     35 fi

Code:
./backup-1.sh[27]: [: ']' missing

Also, is there anyway to find actually which directory is missing and give appropirate message(s)

Thanks in advance.

Last edited by Franklin52; 09-15-2011 at 03:17 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 09-14-2011
&& doesn't work inside [ ].

Your logic is slightly off as well. If dir1 doesn't exist AND dir2 doesn't exist AND dir3 doesn't exist, it'll complain -- if only one or two are missing, it wouldn't.

Better logic would be, if dir1 exists AND dir2 exists AND dir3 exists, then everything's okay, otherwise, error.


Couple ways to do it.

Code:
function check_dirs1
{
        # && means 'if the first is okay, run the second'.  So if any of these don't exist,
        # the chain will stop and the function will return nonzero.
        [ -d dir1 ] && [ -d dir2 ] && [ -d dir3 ]
}

function check_dirs2
{
        # -a instead of && for [ ]
        [ -d dir1 -a -d dir2 -a -d dir3 ]
}

function check_dirs3
{
        # for bash/ksh, which has double-brackets [[ ]]
        [[ -d dir1 && -d dir2 && -d dir3 ]]
}

# If your expression is very large, you can break it into parts
function check_dirs4
{
        # || means, 'if the first thing doesn't succeed, run the second'.
        # So if dir1 doesn't exist, the function will return immediately
        # with a nonzero value.
        [ -d dir1 ] || return 1
        [ -d dir2 ] || return 1
        [ -d dir3 ] || return 1
        # once you're sure they all exist, return okay
        return 0
}

if ! check_dirs1
then
        echo "Required files not found in dirs1"
fi

if ! check_dirs2
then
        echo "Required files not found in dirs2"
fi

if ! check_dirs3
then
        echo "Required files not found in dirs3"
fi

if ! check_dirs4
then
        echo "Required files not found in dirs4"
fi


Last edited by Corona688; 09-14-2011 at 01:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

2. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

3. Shell Programming and Scripting

Csh/tcsh : Check the file existance and run the script

Hi, I've to wait until a file generated and once its generated, source another script in Linux terminal. Please help me as this is very very urgent. The code should be something like if ( -e "/abc/xyz/a.txt ) source aaa.csh else sleep This should be repeated till the if... (4 Replies)
Discussion started by: kumar_eee
4 Replies

4. Shell Programming and Scripting

check for file existance and generate exit code amd move to another directory

Hi.. i have a file ABC_*.txt in source directory which will come evry 30 min and same file will be moved to working directory first time ...and will perform some operations then we archive ABC_*.txt ..this will run for 30 min to 45 min from 2nd time onwards requirement is ...i need to check... (3 Replies)
Discussion started by: dssyadav
3 Replies

5. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

6. Shell Programming and Scripting

Scripting to check the size of file and it's existance.

Hi, I am totaly new to create a script . Please help. I have file name retrived from SAP table into a internal table . Like :- /home/td_8d02_int_data_IPCL/ILLUSTRATIONS/CGM/l_pc_112138_01_0_01_00.cgm /home/td_8d02_int_data_IPC-L/ILLUSTRATIONS/CMP/l_pc_112138_01_0_01_00.cmp Objective... (1 Reply)
Discussion started by: amitkumar.b2
1 Replies

7. Shell Programming and Scripting

Checking the existance of multiple files

I am trying to execute the following command to check the existance of a file (which has a date timestamp on it). If there are more than one file, then also it should give me 'success' result. if then <do some work> else <no files> fi Since there are more than one... (18 Replies)
Discussion started by: vivek_damodaran
18 Replies

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

9. Shell Programming and Scripting

checking existance of files and directories

I have a list of files and their directories, starting from a particular location. e.g. temp/new/a.c temp/new/b.c temp/old/a.c temp/old/older/b.c etc. Now I want to check the existence of all these files in the respective directories . I am trying something like cat "fileList" |... (4 Replies)
Discussion started by: herojeett
4 Replies

10. Shell Programming and Scripting

check the file existance

Hello, I have two files .. 1. inventory_i.txt 2. inventory_b.txt I want to check if these two files exists. If exists, then do the process, otherwise, quite... if ; then echo "exists" else echo " does not exists" fi The above logic is not working... It is always, displaying Does... (2 Replies)
Discussion started by: govindts
2 Replies
Login or Register to Ask a Question