LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2003, 08:39 AM   #1
jojo
LQ Newbie
 
Registered: Feb 2003
Posts: 5

Rep: Reputation: 0
Unhappy 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,
 
Old 02-20-2003, 08:48 AM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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
 
Old 02-20-2003, 09:07 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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.
 
Old 02-20-2003, 09:26 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
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
 
Old 02-20-2003, 11:14 AM   #5
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
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\*
 
Old 02-20-2003, 05:48 PM   #6
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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
 
Old 02-21-2003, 11:27 AM   #7
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
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. . .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace answer word? matarodi Linux - General 0 10-27-2005 01:51 PM
find a word within any files Linh Linux - General 6 06-06-2004 11:15 AM
how can i find all include special word in a dir henryluo Linux - General 4 11-12-2003 10:56 AM
find a word 2 jojo Programming 4 02-23-2003 09:55 AM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-16-2001 11:57 PM


All times are GMT -5. The time now is 01:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration