Execute "find . -exec grep" in desending Order


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Execute "find . -exec grep" in desending Order
# 1  
Old 02-06-2009
Execute "find . -exec grep" in desending Order

Hi Xperts,

I've one query for you. Please help me solving this.

Below command is taking long time to fetch names of the files which contain string "475193976" because folder contains millions of files. I agree that this is what this function suppose to do. Correct.. But can it be possible that this find runs descendingly instead of running ascendingly?

find . -exec grep -l '475193976' {} \;

Lets suppose i've a folder which contain 10 files. Name of these files are below:
date format is (YYYMMDD-HHMMSS)
1st file name = 20080406-011603-132_ICH830_20090206-011519_471
2 = 20080506-011603-132_ICH830_20090206-011519_471
3 = 20080606-011603-132_ICH830_20090206-011519_471
4 = 20080706-011603-132_ICH830_20090206-011519_471
5 = 20080806-011603-132_ICH830_20090206-011519_471
6 = 20081006-011603-132_ICH830_20090206-011519_471
7 = 20081106-011603-132_ICH830_20090206-011519_471
8 = 20081206-011603-132_ICH830_20090206-011519_471
9 = 20090106-011603-132_ICH830_20090206-011519_471
10 = 20090206-011603-132_ICH830_20090206-011519_471

Now if i use the above command "find . -exec grep -l '475193976' {} \;", this will start searchin files from 1 to 10..

My problem is :- Can it run backwards??. i.e 10 to 1. By this way i can get the latest modified file and as soon as i get that file i can cancel the nxt search because my Motive is to find the Latest timestamp file which is "20090206-011603-132_ICH830_20090206-011519_471" in above example. Also there are total of more than 1 lakhs files in folder where we're using find function.

I'll be very thankful If someone can help me in this. ALso i feel i explain the problem is simple and detailed manner just because i can get a good result from you people.

Waiting for your response.

Thanks in Advance.
# 2  
Old 02-06-2009
Well, quick question:

Must the find be used to find files recursively?
ie are their sub-directories you want to search also?
# 3  
Old 02-06-2009
If you don't have to use 'find' then I'd do the following:

cd <mydir>
for FILE in `ls -t`
do
grep -l '475193976' $FILE
done
# 4  
Old 02-06-2009
Quote:
Originally Posted by quirkasaurus
Well, quick question:

Must the find be used to find files recursively?
ie are their sub-directories you want to search also?

No not necessarily. Let it search one directory only.
# 5  
Old 02-06-2009
No not necessarily. Let it search one directory only.
# 6  
Old 02-06-2009
Quote:
Originally Posted by homeyjoe
If you don't have to use 'find' then I'd do the following:

cd <mydir>
for FILE in `ls -t`
do
grep -l '475193976' $FILE
done
unixsearch.file1: 0403-029 There is not enough memory available now.

Im getting this message.
# 7  
Old 02-06-2009
A variation on homeyjoe's solution that won't take up the memory:

Code:
cd <mydir>
/bin/ls -t |
while read file ; do
  grep -l '475193976' $file
done |
  tee /somewhere/else/my_log


Last edited by quirkasaurus; 02-06-2009 at 05:30 PM.. Reason: oops!! log had to elsewhere
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print the "grep" result as specified keyword order?

I have a content.xls file as given below, NC_020815.1 1891831 1894692 virb4_A0A0H2X8Z4_ 1 954 1945 NC_020815.1 1883937 1886123 vird4_A0A0P9KA26_ 1 729 1379 NC_020815.1 2976151 2974985 virb10_H8FLU5_Ba 1 393 478 NC_020815.1 2968797 2967745 virb6_A0A0Q5GCZ4 5 398 499... (2 Replies)
Discussion started by: dineshkumarsrk
2 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Difference b/w xargs and "-exec" in Find

Hi, What is the difference between the following commands find . -type f -exec grep 'abc' {} \; and find . -type f | xargs grep 'abc' Appreciate your help. (2 Replies)
Discussion started by: bobbygsk
2 Replies

4. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

5. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

6. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

7. UNIX for Dummies Questions & Answers

Using "find" and "-exec rm" ... Just no luck :(

Hi All, Ok, so I'm fairly new to Unix, but would like to think I'm grasping things fairly ok (Well until now ;) ), BUT with this one I'm going in circles ... LOL .. really can't work out why? So sorry for this post if it seems a little "dumb" .. But here goes. In short I'd like to achieve the... (55 Replies)
Discussion started by: Dean Rotherham
55 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question