1753658 Members
5313 Online
108798 Solutions
New Discussion юеВ

Find file based on list

 
SOLVED
Go to solution
Michael Allmer
Frequent Advisor

Find file based on list

I have a file that contains a partial file name.
Example name.txt contains
573_-_mm-cubeside180
573_-_mm-cubeside270
573_-_mm-cubeside90
573_-_mm-datum-ring
573_-_mm-datumringstand

I want to use 'find' to located them on disk with adding file extention.
example: find /tool -name 573_-_mm-cubeside180*.prt
And use the output from 'find' to exec ls -l on the full path of the file.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Find file based on list

Hi Mike:

Give your file named 'name.txt' with each line representing a filename, you could do:

while read NAME
do
find /tool -xdev -type f -name "${NAME}*.prt" -exec ls -l {} +
done < name.txt

I use the -xdev' so as not to cross mountpoints. I use the '-type f' to restrict us to files and not directories, etc. The use of the "+' terminator means that multiple arguments (here, filenames) can be collected and passed to one instantation of the command ('ls'). This is quite efficient.

Regards!

...JRF...

Suraj K Sankari
Honored Contributor

Re: Find file based on list

HI,
You can used find command with for loop
i.e.
for i in `cat name.txt`
do
find . -name $i -exec ls -l {} \; >/tmp/find.out
done

Suraj
Dennis Handly
Acclaimed Contributor
Solution

Re: Find file based on list

If you have lots of files or a very large filesystem to search, you don't want to use a stinkin' for loop with find(1).

Possibly something like:
find /tool $(
awk '
BEGIN { print "(" }
{
print "-name " $1 "*.prt -o "
}
END { print " -name bad_Xname )" } ' < name.txt
) -exec ll +
James R. Ferguson
Acclaimed Contributor

Re: Find file based on list

Hi (again):

@ Dennis: That's a very nice solution to keep from abusing the server! :-)

@ Mike: No points for this comment, please.

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: Find file based on list

Thanks for all the assistance.
Dennis Handly
Acclaimed Contributor

Re: Find file based on list

I just realized there is a simpler solution than my awk script above. The key thing is to use vector methods and not loops.
find /tool -name "*.prt" | fgrep -f name.txt | xargs ll