Find all files before a certain date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find all files before a certain date
# 8  
Old 05-31-2012
Quote:
Originally Posted by bakunin
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 hope this helps.

bakunin
Thanks Bakunin.

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.

Zig
# 9  
Old 05-31-2012
Quote:
Originally Posted by SyphaX
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?
Suppose you have a file file with the patterns you search for (the names of the .pdf files). How about something along the lines of:

Code:
 find /some/path -type f -name "*html" -exec grep -f file {} \;

I hope this helps.

bakunin
# 10  
Old 05-31-2012
Quote:
Originally Posted by bakunin
Suppose you have a file file with the patterns you search for (the names of the .pdf files). How about something along the lines of:

Code:
 find /some/path -type f -name "*html" -exec grep -f file {} \;

I hope this helps.

bakunin
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
# 11  
Old 06-01-2012
So based on the above, I made a script that does the work:

Code:
#!/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.

Thanks,
Zig

Last edited by bakunin; 06-02-2012 at 02:27 AM..
# 12  
Old 06-01-2012
It's slow because you're running grep dozens to thousands of times for things it only needs to run a few times for. That's also a useless use of cat.

Your use of -l was a good idea, since it should stop at the first match in the file instead of hunting for all matches in a file.

The matching may not work because they're regular expressions, not filenames. Use -F to force grep to interpret the patterns as fixed strings instead.

Beware that blank lines in the file will mess it up. Everything matches against a blank expression.

Also, use xargs to run grep once for many files, which should help a lot.

Code:
find /some/path -type f -name "*.html"| xargs grep -l -F -f patternfile


Last edited by Corona688; 06-01-2012 at 02:35 PM..
# 13  
Old 06-01-2012
For some reason the 1 line command doesn't work. This is what I have:

Code:
#!/bin/ksh

find /somepath -type f -name "*.html"| xargs grep -l -F -f /path-to-patterns-file {} \; 2>/dev/null 1>>/path-to-results;

The above doesn't return any results which is wrong. I am not sure what's wrong or if I have some syntax error somewhere.

Moderator's Comments:
Mod Comment edit by bakunin: please use [CODE] .. [/CODE]-tags when posting code or terminal output. Thank you

Last edited by bakunin; 06-02-2012 at 02:32 AM..
# 14  
Old 06-04-2012
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

2. Red Hat

Find Files within date Range

Hi i am looking to expand a command i am using to find files in a large file system. i am currently using find /raid/JOBFLOW_LOCKED/ -type f -size +3G | -exec mv {} /raid/JOBFLOW_LOCKED/KILL \; This works really well but i would like to add a date range to the same command to refine it... (6 Replies)
Discussion started by: treds
6 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. Shell Programming and Scripting

Find files for a specific date

Hi, I am looking to find files of a specific date. I am on Sun Solaris so newermt doesnot work.. I thought of using mtime but not getting how to use it. Please help me with this.. Regards Abhinav (3 Replies)
Discussion started by: abhi1988sri
3 Replies

5. Shell Programming and Scripting

Find files with specific date

Dear all, kindly i have some files with different dates i need to grep word from these files but i need to search in files with date 2012-12-02 not all files in this directory do u have any command (4 Replies)
Discussion started by: maxim42
4 Replies

6. Shell Programming and Scripting

Find older files than specified date

Hi, I need to find out list of files which are older than specific date. I am using 'find, and newer' commands but its not giving the correct result. Can you please help to findout the list of files. thanks (2 Replies)
Discussion started by: Satyak
2 Replies

7. UNIX for Dummies Questions & Answers

Find last modified date for many files

Hello all - I've looked and have not been able to find a "find" command that will list the last modified date of files within a specific directory and its subdirectories. If anyone knows of such a command it would be very much appreciated! If possible, I would like to sort this output and have... (5 Replies)
Discussion started by: MichaelH3947
5 Replies

8. Shell Programming and Scripting

Can I know find syntax to find given date files

Hi All, Can i use find command to know given date files? If yes, then please let me know the syntax for the same. Thanks in advance for your postive responses Regards, Bachegowda (3 Replies)
Discussion started by: bache_gowda
3 Replies

9. Shell Programming and Scripting

find files by date

************************************************** Purpose : find files by date Condition: olther than | newer than | between _date1 _date2 Date format: 2007/10/28 ************************************************** Please help me Thanks (1 Reply)
Discussion started by: kani
1 Replies

10. UNIX for Advanced & Expert Users

Find all the files after the date?

Hi I am using #!/bin/sh DATE="$1" FILE="$2" FLIST="" for f in $FILE do FDATE=$(ls -l $f | awk '{ print $6 }') if ;then FLIST="$FLIST $f" fi done && echo $FLIST || echo "Sorry no files found to match $DATE date." the below... need correction whne i execute the above (1 Reply)
Discussion started by: gkrishnag
1 Replies
Login or Register to Ask a Question