Home > Software design >  How to check if a specific folder exists and no other file or folder in same path
How to check if a specific folder exists and no other file or folder in same path

Time:09-28

We have a situation where we need to run specific set of tests, if a folder is present in a specific path.

If other files are folders present along with this folder, we need to run different set of commands.

If {only this folder present}

Run scriptA.sh

Elseif {this folder and other folders/files}

Run scriptB.sh

Fi

CodePudding user response:

Try with:

#!/bin/bash

FOLDER=/some/folder/

if [[ -d ${FOLDER} ]]; then

  if ls "${FOLDER}" 2>&1 >/dev/null; then
    script_for_files_present.sh
  else    
    script_for_NO_files_present.sh
  fi

fi

CodePudding user response:

This code will run scriptA.sh if $folder exist, but if $other_folder also exists it'll run scriptB.sh:

check(){
    [[ -d $folder ]] && {

        [[ -d $other_folder ]] && {
            scriptB.sh
            return
        }

        scriptA.sh
    }
}

check
  • Related