Find command doesn't return in shell script.

Hi All,

I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file.

However, this doesn't work as I believe the find command doesn't exit's inside the shell script. The output file to which the find command redirects remains empty, even though the find command when run on the shell, returns rows.

for j in `cat searchstring.out`;
do

find / -name *$j* > $HOME/match.out

for k in match.out ;

do
owner=$(ls -l $k | awk '{print $3}')

echo "File found is:- $k"
echo "Owner is:- $owner"

done
done

Can someone advise how this can be made to work? How does the find command store all output to a file?

Thanks in advance.

The * must be evaluated by the find command, you must escape it from the shell.

find / -name \*$j\*

where the $j is still evaluated by the shell. If this is not wanted, use a " " escape.

find / -name "*$j*"

Thanks, tried this. It doesn't help in writing the output from find to write to an output file however.

A second error is

for k in match.out ;

You certainly want

for k in `cat match.out`

like you did in the outer loop.
But this is not ideal for many reasons. A more efficient and less risky version:

while read j
do
  find / -name "*$j*" |
  while read k
  do
    owner=$(ls -ld "$k" | awk '{print $3}')
    echo "File found is:- $k"
    echo "Owner is:- $owner"
  done
done < searchstring.out

(NB HP-UX find does not have -ls)

Thanks.

This looks much cleaner now. But when I try this, the search string is read from the output file correctly but seems the output from find is not piped to the second while loop. So, neither do I get the file found, nor the owner displayed on console.

When I run the find manually on shell (using same search string), it indeed returns the results.

Any idea what could be more wrong here?

Please confirm.

Regards.

No, j is a variable.
The whole outer loop is fed from searchstring.out , see the redirection at the end!

The inner loop is fed from the find command. The | conects the find command with the loop.

You're missing a \ after the | .

Another question, do you really want to use find to search throgough / ?
Wouldnt locate be a bit faster?

If HP-UX find has -printf , and with all the known caveats for xargs , and out of sheer curiosity, this might yield the same result as above:

< searstring.out xargs -n1 -I{} find . -name {} -printf "File found: %p; owner %u\n"

Hi Guys,

Really need this to work by tomorrow. Is there any fool proof method that you might be aware of which would help achieve this?

Please confirm and throw some light.

Thanks in advance.

Regards.

locate's results are usually only updated once a day.

Hi Sea,

I added "\" after "|" but still I dont see "find" returning any output.

Can you please suggest if there should be any other change required?

Thanks.

As you said, a "\" after "|" does not make a functional difference.
Is your searchstring.out file okay?
Look at it with an editor on Unix.
Not created from WinDOS that appends invisible ^M (CR) characters?
(If yes, delete them.)
Add a test line with just an e . Then the find will run on *e* and certainly find many files.