Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a directrory for unlimited number of files as arguments 1

Status
Not open for further replies.
Apr 8, 2001
8
0
0
US
Hi,

I want to write a script that will be invoked at command line by:

searchdir.ksh /home/stoteve file1 file2 file3

Where I can type as many file arguments. Then the script will read the directory as argument $1 then loop through all the other arguments searching for the file. I would like the output to goto a file called find.file then I could cat it or something. Have not thought about that as I can't figure out how to write this script where it will pass the arguments into it. Does anyone have a script that does this, that I can leverage off of?

Thanks,

Sam
 
man find (-name primary)
man ksh (ALL, especially shift and for)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You probably know this and forgive me if I've misunderstood your post. :) But you can search the entire server for a file with the following command:

find / -name "word"

or you can search from where you are "at" if you do

find . -name "word"

This will list every path where a file exists that has "word" in it.
 
Oh. I get it. Sorry. You are looking to do several 'find . -name "word"' at one time so that you can search for the instances of more than just one file right?

Well I don't know the code off the top of my head, but in perl you can use the @ARGV array to determine the number of arguments on the command line and then you could create a loop that access each element of the array (ARGV[0], ARGV[1] ...) and etc.

Hope this helps. If this isn't what you are trying to do this, just ignore this post. ;)
 
Q & D, without error checking:

Code:
#!/bin/ksh

DIR=$1
shift

OPTS="-name '$1'"
shift

while [[ "$1" != "" ]]
do
OPTS="$OPTS -o -name '$1'"
shift
done

eval "find $DIR $OPTS"

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
Thanks Rod,

I need the first variable so shift would not delete it. How would I validate the directory from the command line? Thus if the directory does not exist then "You entered a invalid Directory", then I can exit the script and make them start over.

Sam
 
man test

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top