LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep recursive usage (https://www.linuxquestions.org/questions/linux-newbie-8/grep-recursive-usage-4175439994/)

dragonix 12-04-2012 08:11 AM

Grep recursive usage
 
Hi all

The cause of my problem is most likely the amount of folders/files..

Anyway, so what I try to do is to search for a combination within a file (grep...)
But I have multiple folders (let's say almost 1000 :) ) and the way I use it, is as follows

Code:

grep -lr <what_to_look_for> ./
After entering, he gives me the first result almost instantly, but then nothing more (waiting couple of minutes now..)

Did I do something wrong?

Thanks!

millgates 12-04-2012 08:23 AM

Depending on how many files, what kind of files and size of the files in your directories, this may indeed take some time to finish. Also, if you have any symlinks in your directories, it may cause problems.

dragonix 12-04-2012 08:36 AM

The files itself are not that large (couple of KB, smaller than 10-20KB)
But I do have a large amount of files that needed to be checked.
So I would think that he would show the ones he has checked, or is that only after the command has ran completely?

TobiSGD 12-04-2012 08:57 AM

grep reports only the files which contain the search pattern, but not the files that don't. So if there are no files with the pattern it will not report anything.

rknichols 12-04-2012 09:43 AM

Also, if grep encounters any FIFOs or sockets in the area being searched, it can sit forever waiting for input. You would need to use the "--devices=skip" option to prevent that.

chrism01 12-04-2012 05:59 PM

grep shows matches as they are found, so you need to wait for the prompt to re-appear, unless it has the problems mentioned by rknichols.
You can avoid those by using 'find' and specifying '-type f', so it only checks regular files.

dragonix 12-05-2012 12:34 AM

Quote:

Originally Posted by TobiSGD (Post 4842567)
grep reports only the files which contain the search pattern, but not the files that don't. So if there are no files with the pattern it will not report anything.

Yes, I know but I am more than 100% sure that he HAS to find more results and trust me I know ;)
But thanks for the pointer!

Quote:

Originally Posted by rknichols (Post 4842608)
Also, if grep encounters any FIFOs or sockets in the area being searched, it can sit forever waiting for input. You would need to use the "--devices=skip" option to prevent that.

True, I haven't tried it yet but I think it is most unlikely that there are any devices in that folder.. But I will give it a shot!
Thanks!

Quote:

Originally Posted by chrism01 (Post 4842849)
grep shows matches as they are found, so you need to wait for the prompt to re-appear, unless it has the problems mentioned by rknichols.
You can avoid those by using 'find' and specifying '-type f', so it only checks regular files.


I tried what rknichols suggested, but it has the same results.. :(
Code:

find . -type f -exec grep -lr --device=skip <criteria> {} \;

pan64 12-05-2012 12:46 AM

If you assumed grep failed to find some files try to execute it in a smaller directory (containing your suspects). Probably your regexp is not perfect.

TobiSGD 12-05-2012 12:47 AM

Quote:

Originally Posted by dragonix (Post 4842985)
Yes, I know but I am more than 100% sure that he HAS to find more results and trust me I know ;)

Show us the actual command you use, including the search pattern and an example file (or the relevant part of a file) that contains that pattern, but is not found by your command.

dragonix 12-05-2012 01:04 AM

Quote:

Originally Posted by TobiSGD (Post 4842995)
Show us the actual command you use, including the search pattern and an example file (or the relevant part of a file) that contains that pattern, but is not found by your command.

The actual command
Code:

$ find . -type f -exec grep -lr --devices=skip "21200" {} \;
Small partion of a file
Code:

<App_Data App="MOD" Name="Type" Value="title"/>
<App_Data App="MOD" Name="Advisories" Value="AL"/>
<App_Data App="MOD" Name="Aspect_Ratio" Value="16:9"/>
<App_Data App="MOD" Name="Billing_ID" Value="21200"/>

So the last part is the thing I'm looking for.

The folder structure is as follows
Code:

/directory (here is where I do my find/grep)
/directory/directory1
/directory/directory1/file1
/directory/directory1/file2
/directory/directory1/file3
/directory/directory2
/directory/directory2/file1
/directory/directory2/file2
/directory/directory2/file3
...


pan64 12-05-2012 01:32 AM

probably you can try grep -lrF 21200 to speed it up a bit

dragonix 12-05-2012 02:03 AM

Seems to give me the same result...
And please, trust me when I see that he should be giving more solutions :p

But I will try it first in a smaller directory

EDIT
Tried it in a smaller folder-structure and there it seems to go fine..
So the problem is that the original folder-structure is too large... (did a count -> almost 2000 folders :D)

millgates 12-05-2012 02:56 AM

Quote:

Originally Posted by dragonix (Post 4842998)
Code:

$ find . -type f -exec grep -lr --devices=skip "21200" {} \;

Out of curiosity, why do you use both find and grep -r?

dragonix 12-05-2012 03:02 AM

Quote:

Originally Posted by millgates (Post 4843043)
Out of curiosity, why do you use both find and grep -r?

Not sure how to look IN a file with the find-command and without grep.
But if you mean why I use the -r argument, tbh I thought, meh it doesn't hurt to use it again (while I know that the find commands looks in every directory beneath the one you specified, but correct me if I'm wrong ;))

chrism01 12-05-2012 04:16 AM

You don't need to use -r for grep; let find take care of recursion.


All times are GMT -5. The time now is 08:17 PM.