Results 1 to 10 of 14
Hi folks,
What will be an easy and effective way searching file content? E.G I need to find a WORD or a PHRASE on a file? TIA
B.R.
satimis...
- 07-26-2008 #1Linux Guru
- Join Date
- Sep 2004
- Posts
- 1,531
How to search file content
Hi folks,
What will be an easy and effective way searching file content? E.G I need to find a WORD or a PHRASE on a file? TIA
B.R.
satimis
- 07-26-2008 #2
You use grep. Open a terminal and type grep word filename. It will print out the line containing the search term (if it is present).
For a phrase containing spaces, you will need to put the whole thing in quotes.
You can also search for strings with partial wildcards. Read the man page for grep for more info."I'm just a little old lady; don't try to dazzle me with jargon!"
- 10-22-2008 #3Linux Guru
- Join Date
- Sep 2004
- Posts
- 1,531
Hi hazel,
Thanks for your advice.
I tried to find files containing the word 'namecard' on all files /path/directory;
# grep -iR namecard /media/usbdisk/Documents_2006_2008/The output just repeats with the mouse pointer pending there.Code:/media/usbdisk/Documents_2006_2008/Fedora/Server/Gani/gani_20070326.txt:pendrive and namecard CD. The complete OS is limited to 50MB.
Could you please shed me some light what does it means? How to proceed?
TIA
B.R.
satimis
- 10-22-2008 #4
As far as I know, that's mean a file name gani_20070326.txt does contain word "namecard" which in the sentence "pendrive and namecard CD. The complete OS is limited to 50MB." Maybe to make grep give more clearer result you may add -n. That's for line number.
So the syntax will be like this:
grep -niR namecard /media/usbdisk/Documents_2006_2008/
- 10-22-2008 #5Just Joined!
- Join Date
- Aug 2008
- Posts
- 9
one could also do
cat /media/usbdisk/Documents/*.* | grep -niR 'whatever'
- 10-22-2008 #6Linux User
- Join Date
- Jun 2007
- Posts
- 318
If you want to know just the name of the files that contain the phrase then use the -l option:
Code:grep -liR namecard /media/usbdisk/Documents_2006_2008/
- 10-23-2008 #7Linux Guru
- Join Date
- Sep 2004
- Posts
- 1,531
- 10-23-2008 #8Linux Guru
- Join Date
- Sep 2004
- Posts
- 1,531
- 10-23-2008 #9Linux Guru
- Join Date
- Sep 2004
- Posts
- 1,531
- 10-23-2008 #10Linux User
- Join Date
- Jun 2007
- Posts
- 318
I don't quite understand. First you were searching for the string 'namecard' in files and now you want to search a directory called namecard? If so, and that directory is a sub-directory of the previous one then the command would look like:
Notice that I took out the -R option so as not to do recursive directory searches. This required adding an '*' to the end so it'll search all files.Code:grep -li namecard /media/usbdisk/Documents_2006_2008/namecard/* or grep -li namecard /media/usbdisk/Documents_2006_2008/*namecard*/*


Reply With Quote

