I am trying to clean up old/orphaned pdf files. I was wondering, is there a way I can run a grep while the find is running to search if each returned file name is linked from an HTML page?
I think I know how to do them individually but not sure how to do both at the same time.
Please post what Operating System and version you are running and what Shell you use. There is some variation in the unix "touch" command and there is a lot of variation in the "find" command.
You might want to read this thread with a short explanation about how to combine various clauses of find . The thread deals with exactly your problem, btw..
On second thoughts you can write the output of the first "find" to an intermediate file and then use "grep -f" to read the patterns you search for from this file. Have a look at the man page of "grep" and look for the "-f" option, which is required by POSIX, so it should be there.
I ran the find pdf on its own and saved the output to a file. I have all the PDFs paths/filenames that meet my criteria.
When I was looking at grep -f, I could only find examples of reading pattern from file and searching in another file. How can I make it search the patterns in an entire folder?
So far all I find is similar to: grep -f file1 file2
Apologies in advance. I am very new to this.
I tried the above and the grep command doesn't seem to work properly. I added a pdf to the pattern file that is linked from other pages but it wasn't found by the grep.
I am starting to think that the format of the patterns in the file matter. Is it possible it fails because of the format of the filenames in the pattern file? ie: abc-abc.pdf
So based on the above, I made a script that does the work:
#!/bin/ksh
for p in $(cat "path-to-patterns-file); do
echo $p;
find ./ -type f -name "*.html" -exec grep -l $p {} \; 2>/dev/null 1>>/path-ro-results;
done
I tested the above and it seems to work accurately. The PDF names are grabbed from the patterns file and the loop is working as expected as well. However, I calculated the amount of time it takes to complete a search for 1 file and it was 45 seconds. If I were to use 2000 pdfs file in the pattern, it would take 24 hours to complete all the searches.
I was wondering, is there a way to further optimize the script and speed it up. I was thinking for example if there is a way to stop the search once a single match is found and jump to the next pattern. I am only interested in PDFs with no results.
I figured out why the command was failing. The box has two grep utilities and the default one didn't support -f option. Once I used the other one the script worked as expected. Now I just need to fix the output so I know which string is being searched for and which file they were found in.
I want to thank everyone who helped me with this script. I appreciate it.
Zig