1833874 Members
2032 Online
110063 Solutions
New Discussion

Re: Find

 
johnreid
Contributor

Find


Hi all,

Find a particular file a unknown path . this I can do using

Find / -name “name of the file”

Also I would like to find a particular string in that specified files.


6 REPLIES 6
Pete Randall
Outstanding Contributor

Re: Find

find / -exec grep -l "string" {} \;


Pete

Pete
Sandman!
Honored Contributor

Re: Find

find / -type f | xargs grep -il "string"
Raj D.
Honored Contributor

Re: Find

Johnreid,

Also remember this will take considerably longtime if you are using from root (/),

Also you can use :

# ls -lR | awk '{print $9}' | xargs grep -l "expr"

This will give you the file names with the matching string,


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Find

John,

In the above example, "expr" is the 'string' that you are looking for in all the files recursively.

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: Find

Hi:

If you are searching for files in the root ('/') directory, you would be advised to add '-xdev' so that you do not cross mountpoints.

Secondly, either pipe 'find's output to 'xargs' or terminate the '-exec' argument with a plus sign ('+') rather than a semicolon (';'). Using 'xargs' or using a plus sign character greatly improves performance because multiple arguments are processes instead of spawning a new process for every, single argument.

Thus:

# find / -xdev -type f -name "*my*" -exec grep pattern {} /dev/null \+

Prior to 11i, you use a '\;' instead of '\+' to terminate the 'find' and suffer the performance penality.

Otherwise:

# find / -xdev -type f -name "*my*" | xargs grep pattern

Note the addition of the '/dev/null' to the form using '-exec' causes the matching filename to be printed for positive 'grep's.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Find

Hi,
to search string "hello world" only in text files including subdirectories:

grep "hello world" $(find ./ -type f |xargs file|grep text|cut -d ':' -f1)

./ = start directory
-type f = only files
grep text = only text files
cut -d ':' -f1 = name of the file

this will avoid to look into other files than text i.e.: binary, links and so on.

HTH,
Art