Check if folder exist using case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if folder exist using case
# 1  
Old 12-09-2013
Check if folder exist using case

Is it possible to use case when checking if a folder exist?

I need to check if these folder exist [5 folders to be exact]. All of them could be present but I still need to check. If a folder is present, something will be processed. Different folder do different stuff.

is this possible?'

Thanks
# 2  
Old 12-10-2013
A case statement is for testing multiple values of one variable. I fail to see how that could be useful for this particular problem.

Perhaps you could give us some pseudo code for what you want your script to do when each of the 5 folders exists or does not exist.
# 3  
Old 12-10-2013
maybe something like this...

Code:
location=/home/myhome/dir
 
case $stuff in
     $location/folder1)
        #do something here
        ;;
     $location/folder2)
       #do something here
       ;;
    $location/folder3)
       #do something here
       ;;
    $location/folder4)
       #do something here
       ;;
   $location/folder5)
       #do something here
       ;;
esac

I want to check the existence of each folder then do something if folder is present.

Is this possible? or should I just use 5 ifs? any suggestions?

Thanks
# 4  
Old 12-10-2013
What is the range of the variable stuff?

Were you thinking:-
Code:
location=/home/myhome/dir

for stuff in *
do 
   if [ -d $stuff ]
   then
      case $stuff in
           $location/folder1)
              #do something here
              ;;
           $location/folder2)
             #do something here
             ;;
          $location/folder3)
             #do something here
             ;;
          $location/folder4)
             #do something here
             ;;
         $location/folder5)
             #do something here
             ;;
         *)
             #Do something for any other value that is a directory
             ;;
      esac
   else
      # Do something for an item that is not a directory
   fi
done



Have I missed the point?

Of course, this will not work with files/directories starting with a full-stop, e.g. .profile




I hope that this helps
Robin
Liverpool/Blackburn
UK
# 5  
Old 12-10-2013
I'm still not quite sure what you are trying to do - perhaps you could define a function to test something is in a given folder like this:

Code:
location=/home/myhome/dir

function stuff_in
{
   for f in "$1"/*
   do
      [ -f "$f" ]
      return
   done
}

if stuff_in "$location/folder1"
then
   echo "$location/folder1 is populated"
fi

if stuff_in "$location/folder2"
then
   echo "$location/folder2 is populated"
fi

if stuff_in "$location/folder3"
then
   echo "$location/folder3 is populated"
fi

Again ignoring files starting with . (like .profile)
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

Checking of file exist in different folder

Hi All, Seeking for your assistance to compare if the file in working directory is found on the DIR2 directory and if not found move the file in DIR2. ex. WORKING_DIR=/home/dir1/ file1.txt DIR2=/home/admin/users file1.txt what i did was found_nonempty='' for file in... (4 Replies)
Discussion started by: znesotomayor
4 Replies

3. Shell Programming and Scripting

file exist in folder

Hi guys, I am new to shell script, i want to write shell script, the script details has mentioned below Two files are to be check in one particular directory, if the file exist in particular folder then the file is existed and also if the file exist more than three hours then send... (2 Replies)
Discussion started by: madhavi_bodi
2 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

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 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