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
A few things stand out:
Code:
thisDIR=$1
I seriously doubt you want a nested call to the function to stomp on a variable that is still in use by its parent. This approach requires that each instance of the function maintain its own private version of $thisDIR; the variable needs to be local.
Code:
for eachFile in `ls $thiDIR`
Shouldn't that be "$thisDIR"? Also, note that using command substitution in that manner means that it is impossible for the loop to properly handle filenames with IFS characters (by default, space, tab, and newlines).
Code:
[ -d $eachFile ]
The "ls $thisDIR" command returns basenames, not an absolute path. Your code does not cd into the directory being ls'd; the -d test will never be correct, as it will be testing for the presence of a directory whose name was taken from a location other than the current working directory (which with the cd commented out, will remain unchanged for the duration of the script's execution).
I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the:
-file name
-line number
-number of matches to the pattern /comp/ for each line.
Two example files:
cat... (4 Replies)
I am new to shell scripting. Can someone help me out with this one please?
I need to write a script fot the following scenario:
I am currently in /parent directory.
I have a set of files in /parent/error_files directory
My script has to search for a file in /parent/erratic_files... (1 Reply)
Hi,
I am trying to write a script that will search in a particular directory and tell me how many files are in there. I then want to be able to email certain users of how many files are in that directory and what the file names are?
any help would be great as i am getting confused.
thanks (3 Replies)
hey,
i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Dear Folks,
I have to backup pgsql database dump everynight on a routine. The database dump actually contains sql(text) statements. The actual size of the database dump is aroung 800 MB. Between two days backup, only few lines of statements are modified/added/deleted.
I dont want to do... (1 Reply)
May be a simple question for experts here....
I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories.
Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Hi everyone,
I need to search files starting with RPT_0, RPT_1,........ in a directory. How can I implement that through a shell script. Also I want to read the last line of each file after searching them. Can someone help me out in this regard.
One more thing how I can extract a particular... (7 Replies)