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