LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2010, 03:22 AM   #1
vikashtulsiyan
LQ Newbie
 
Registered: Oct 2007
Posts: 4

Rep: Reputation: 0
recursive searching files in directory matching given name - taking care of links


Hi,
I am writing a shell script that finds all files named <myFile> in a directory <dir> or any of its subdirectories, recursively. I also need to take care of symbolic links that may form cycles, to avoid infinite loops. I am not supposed to use find command for the same

I started writing the code but got stuck. I thought using recursion may be a smart way, but its not working. Kindly help
Code:
#!/bin/sh


findFiles()
{
thisDIR=$1
#cd $thisDIR
for eachFile in `ls $thiDIR`
do
	if [ "$eachFile" = "$FILE" ]; then
		echo "$FILE found in $thisDIR"
	elif [ -d $eachFile ]; then
		findFiles ${thisDIR}/${eachFile}
	fi 
done 		

}
if [ $# -ne 2 ]; then
	echo "Please run the script as $0 NameOfFile PathToDirectory"
	exit 1
fi

FILE=$1
DIR=$2

findFiles $DIR
 
Old 04-16-2010, 06:19 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
have you looked at grep with -R option

Code:
grep -r <word> dir/*
 
Old 04-16-2010, 06:30 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Also with your coding, unless a requirement of the class you are taking, why is FILE treated as a global
variable but DIR is then used as an argument?
 
Old 04-16-2010, 06:40 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by vikashtulsiyan View Post
Code:
findFiles()
{
    local thisDIR=$1
    #cd $thisDIR
    for eachFile in `ls $thiDIR`  # wrong variable name  
    do
    	if [ "$eachFile" = "$FILE" ]; then
    		echo "$FILE found in $thisDIR"
    	elif [ -d $eachFile ]; then
    		findFiles ${thisDIR}/${eachFile}
    	fi 
    done 		
}
For recursion to work you need local variables otherwise each new instance of the function changes variables used by its ancestors.

Also you have a mis-typed variable name. Useful to use set -o nounset to make bash tell you about unset variables (including mis-typed names).
 
Old 04-16-2010, 06:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@catkin - I guess I was being too subtle
 
Old 04-17-2010, 01:17 AM   #6
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
I was playing with this yesterday, declared the function variables as local, but still couldn't get it to go down more than two directories. The directory test line just fails the test.


Code:
  +--test        <--start here
   |
   +--dirone     <--descends to here
    |
    +--dirtwo    <--ignores this dir
     |
     +--dirthree
 
  


Reply

Tags
files, links



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Got to be a better way. Perl recursive directory listing (not files) PB0711 Programming 8 03-16-2009 05:24 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
What else besides POOLING a directory searching for files? richikiki Programming 1 08-19-2006 11:43 AM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 12:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration