How to search for particular file?

Go down

How to search for particular file?

Post  Ramprasadg on Mon Aug 18, 2008 7:50 am

How to search for particular file that contains a few keywords?(remember i am not talking about file names)
in windows search there is this option that can search the content ,how to do it in Linux
avatar
Ramprasadg

Posts : 12
Join date : 2008-08-13

View user profile

Back to top Go down

Re: How to search for particular file?

Post  vijay on Mon Aug 18, 2008 8:25 am

probably u've to use find(1) along with grep(1)....

a sample could be...
Code:

for i in `find /home/ram -name "*.txt" -print`
do
    if grep great $i 1>/dev/null 2>&1
    then
          echo $i
    fi
done

the above shell sequence searches in each file whose name matches *.txt under the directory /home/ram... and prints the filename if it contains "great" pattern in it....

remember... find(1) is recursive... i.e it searches the sub directories as well.....

or a better version of the above example could be....with grep(1)'s -l option which is used to print just the filenames instead of the lines which match the given regular expression....
i.e
Code:

$ grep -l <regExp> `find /home/ram -name "*.txt" -print`

Hope that helps.....

vijay

Posts : 6
Join date : 2008-08-18

View user profile

Back to top Go down

grep

Post  csbhagav on Mon Aug 18, 2008 9:44 pm

grep is capable enuf to srch for text inside a file .


grep <keyword> <filename> can do it.

csbhagav

Posts : 3
Join date : 2008-08-18

View user profile

Back to top Go down

Re: How to search for particular file?

Post  Beagle on Tue Aug 19, 2008 2:59 am

consider this...

Search pattern - abc

filename - <whatever>

if i use $grep <search pattern> <filename>, it will list all the occurences of abc anywhere in the line of file

like the entries "xyzabc123" and "abc" alone would result in the finding of abc pattern. But what i can get from the problem is that i have to search for the abc pattern alone.

above example would search twice for this pattern however the command should have reported only one single occurence alien

Beagle

Posts : 9
Join date : 2008-08-11

View user profile

Back to top Go down

grep -e

Post  csbhagav on Tue Aug 19, 2008 7:45 am

considering ur file name to be a.txt and content to be searched is abc


this command will do it :

grep -e [[:space:]]abc[[:space:]] a.txt

-e is used to specify the pattern to be a regular expression.

this will print the whole line . u can print jus the filename using -l option.

csbhagav

Posts : 3
Join date : 2008-08-18

View user profile

Back to top Go down

A program with gui ( beagle )

Post  Ramprasadg on Tue Aug 19, 2008 2:58 pm

name: beagle
for more details http://linuxappfinder.com/blog/linux_desktop_search

please continue the discussion...
avatar
Ramprasadg

Posts : 12
Join date : 2008-08-13

View user profile

Back to top Go down

Re: How to search for particular file?

Post  ankitgr8 on Sun Sep 07, 2008 8:23 am

grep -l 'pattern' *
this will give the names of all the filenames containing the pattern

ankitgr8

Posts : 6
Join date : 2008-08-11

View user profile

Back to top Go down

Re: How to search for particular file?

Post  Gyanendra on Sun Sep 07, 2008 9:20 pm

Plzz add few more questions ..and continue the topic!! cheers

Gyanendra

Posts : 8
Join date : 2008-09-07

View user profile

Back to top Go down

Re: How to search for particular file?

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum