LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Find a word. (https://www.linuxquestions.org/questions/programming-9/find-a-word-46311/)

jojo 02-20-2003 08:39 AM

Find a word.
 
Hi All,
I want to find all the files in my system that contains a specific word(exact match).

i wote this command but it didnt work out:


find / -type f -exec fgrep -l "my_word" {} \;>/tmp/my_file

my_file-should contain a list of files that contains the word that i'm looking for.


hope someone can help........

Thanks,
:confused:

wapcaplet 02-20-2003 08:48 AM

I haven't tried this, but I think you need to put the {} and ; in quotes, like this:

find / -type f -exec fgrep -l "my_word" '{} ';' > /tmp/my_file

The quotes, I think, prevent them from being interpreted literally by find (or maybe by fgrep)

The Linux Cookbook has a nice section on finding files, which is where I got this info from. Check it out if you haven't already :)

crabboy 02-20-2003 09:07 AM

Your find statement works correctly. You probably need to setup a controlled environment to test. ie. search a smaller directory with files that guarantee a hit.

Mik 02-20-2003 09:26 AM

Just something you might want to consider since you plan for it to search your whole filesystem which could be a lot of files.

The find command is gonna start a fgrep process for each file it finds and then look through it and then the fgrep process will close again. It's gonna repeat this till it's gone through each file it's found.
If you use the xargs command then a whole list of files can be sent to one fgrep process which reduces the overhead of having to stop and start so many processes. Ofcourse you get the added overhead of xargs and the pipe, but that's a lot less.
The nice thing about *nix is that it's good with handling starting and stopping of processes so you don't notice it that much. But if you try to run the same thing on Windows NT with cygwin then you really start noticing the difference, if it doesn't lock up on you in the mean time.
Anyways here is what the command with xargs would look like, which in my opinion also looks a lot simpler:

find / -type f | xargs fgrep -l "my_word" > /tmp/my_file

moses 02-20-2003 11:14 AM

In my opinion, there's an even easier way to use find to find a filename that you are looking for:

Code:

find / -type f -name my_word
You can also use \* as a glob to find all instances of files that
contain the string my_word:
Code:

find / -type f -name \*my_word\*

wapcaplet 02-20-2003 05:48 PM

Quote:

Originally posted by moses
In my opinion, there's an even easier way to use find to find a filename that you are looking for
I think jojo was trying to find files (such as text files) that *contain* a certain word, though, rather than a matching file name. So find -name won't quite do it :)

moses 02-21-2003 11:27 AM

Quote:

Originally posted by wapcaplet
I think jojo was trying to find files (such as text files) that *contain* a certain word, though, rather than a matching file name. So find -name won't quite do it :)
Right. Maybe I should sleep more. . .


All times are GMT -5. The time now is 04:56 AM.