Find file in a directory

Hi, I'm trying to write a script that search file in folder.
I got problems with selecting specific file from the list of the files.
when I runing the script I get a list of files instead of one specific file.
I'm new with linux :slight_smile:

#!/bin/bash

FILE=false
ISFOUND=false

while getopts f:d: OPTION
do
    
case $OPTION in
    
	f) FILE=$OPTARG ;;
    	d) dirVar=$OPTARG
     esac
done

IFS=$'\n'

for file in "$(ls -1 $dirVar)"   ; do
           if [  "$file" = "$FILE"  ] ; then
              ISFOUND=true
     	      echo "found file : $FILE"
	  fi
done

unset IFS

if [ "$ISFOUND" = "false" ] ; then
    echo "file does not exists"
fi

this should work

#!/bin/bash
while getopts f:d: OPTION
do
	case $OPTION in
		f) FILE=$OPTARG ;;
		d) dirVar=$OPTARG
     esac
done
ls "$dirVar/$FILE" && echo "found file : $FILE"

thanx for the replay.
I tried your code and if I enter the Input

./FindFile.sh -d home/student/TA2 -f s1.sh

I get :
ls: cannot access /s1.sh: No such file or directoty

what should I do?

shouldn't the directory be /home/student/TA2 ?

I tried to run it like u said with */*home/
and still got the same problem.

when I'm trying to get only one specific file from the directory as I tried earlier
my output is a list of all the file in the directory.

If it gives you a list of files it's because your directory/file specifications corresponds to more tyhan one file. With wildcards like '*' it's not surprisingly.

Ok I succeed to find a file in a folder
and to declare if the "file" that I found is a folder or not.

now I'm trying to get in the subdirectories and to search there recursive,
I tried to write the scrip but I'm getting bugs.

Don't know where's my problem...need help with that please!
thanx!

here is my script.

#!/bin/bash


FILE=false

ISFOUND=false


while getopts f:d: OPTION

do

    case $OPTION in

    f) FILE=$OPTARG ;;

    d) diyrVar=$OPTARG

     esac

done

function search ()

{

    IFS=$'\n'

    for file in $(ls -1 $dirVar)   ; do

      if [ "$file" = "$FILE"  ] ; then

	  if [ -d $file ] ; then         #is a folder 
	 
		#search $dirVar/$file    #go recurcive to the folder

              echo "$file is a folder"
	      
	   else              #is not a folder

               ISFOUND=true
     	 
	       echo "found file : $FILE"

	   fi
       fi	  
 
   done

   unset IFS

 
	if [ "$ISFOUND" = "false" ] ; then

		echo "file does not exists"

    	fi

}

search