Find file in a directory


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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 Smilie


Code:
#!/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


Last edited by vgersh99; 03-23-2010 at 08:32 AM.. Reason: code tags, please!
# 2  
this should work
Code:
#!/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"

# 3  
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?
# 4  
shouldn't the directory be /home/student/TA2 ?
# 5  
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.
# 6  
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.
# 7  
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.

Code:
#!/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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #79
Difficulty: Easy
GNU/Linux is directly descended from BSD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find and get a file in an entire directory with an excluded directory specified?

How to get a file 'zlib.h' in an entire directory with an excluded directory specified lives under that starting directory by using find command, as it failed on: $ find . -name 'zlib.h' -a -ipath 'CHROME.TMP' -prune -o -print it'll just list entirely up (2 Replies)
Discussion started by: abdulbadii
2 Replies

2. Solaris

/bin/find: stat() error <File> : No such file or directory

Hi, I am getting below error in Solaris 10 SPARC when trying to issue a search on /var/tmp partition Below is the query /bin/find /var/tmp/ -type f -atime +1 Below is the result /bin/find: stat() error <File> : No such file or directory (28 Replies)
Discussion started by: prash358
28 Replies

3. UNIX for Dummies Questions & Answers

Find the location of particular file and directory

hi all, i am new to UNIX environment. i have a file and directory with same name, i don't know the location I want to find location of that file and directory. please suggest a solution. (5 Replies)
Discussion started by: mahesh1987
5 Replies

4. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

5. UNIX for Dummies Questions & Answers

cannot find file directory

hi, i use command at below to copy the file and put it in the new file (if no such file, it will create right ) cat id_rsa.pub >>~/.ssh/authorized_keys it give me error bash: /home/pc2/.ssh/authorized_keys : No such file or directory can anyone tell me why this happened ? thanks in... (1 Reply)
Discussion started by: Ericyue
1 Replies

6. Shell Programming and Scripting

Find file only from a Directory

Hi, I am using the below query to find files from a Directory: SOURCE_DIR--Directory Files in the Directory: ABC.log XYZ.out The SOURCE_DIR has subdirectory.... when i use the below command... find /apps/informatica/node1/scripts/test '(' -name '*.log' -o -name '*.out'... (12 Replies)
Discussion started by: ch33ry
12 Replies

7. Shell Programming and Scripting

Find file directory

Hi, I am using the below command for searching the file var_SearchFile= find $var_DataSourcePath -name $var_SourceFileName Output: c:\test\Sample\Testfile.txt But I need to retrive only the Path that is "c:\test\Sample\" (8 Replies)
Discussion started by: magesh_bala
8 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

10. UNIX for Dummies Questions & Answers

how to find a corrupted file in a directory

Hi, Dunno if this has already been discussed but can anyone tell me if there is any utility/command to find a corrupted file in an unix directory. With smile, karthik (1 Reply)
Discussion started by: skarthik_d
1 Replies

Featured Tech Videos