1
I'm trying to search the entire file system for specific text. This command does that but it gets hung up in certain directories like '/proc':
find / -print0|xargs -0 grep whatever
What I'd like to do is only send files to grep that don't match '/sys' '/proc' '/tmp' '/lib'.
Update: After the help from Gary below I needed to add a few more excluded directories so I decided to write a python script to do this:
#!/usr/bin/python
import sys
import os
from os.path import join, getsize
import time
import re
search = "192.168.30"
searchRoot = "/"
reobj = re.compile(r"^/var|^/dev|^/proc|^/sys|^/bin|^/boot|^/home|^/lost|^/media|^/misc|^/mnt|^/net|^/sbin|^/selinux|\.log")
start = time.time()
for root, dirs, files in os.walk(searchRoot):
for name in files:
fullPath = os.path.join(root, name)
if not reobj.search(fullPath):
try:
fileSize = os.path.getsize(fullPath)
if (fileSize < 51200):
try:
#print fullPath
fileobj = open(fullPath, 'r')
text = fileobj.read()
fileobj.close()
index = text.find(search)
if (index > -1):
print index, ":", fullPath
sys.stdout.flush()
except:
pass
except:
pass
print "Completed search for", search, "in", searchRoot, "in", time.time() - start, "seconds."
Looks like my version of grep doesn't support that argument. I'm on Red Hat Enterprise Linux Server release 5.6. This is the man page for the version I have: http://unixhelp.ed.ac.uk/CGI/man-cgi?grep .
– Andy Arismendi – 2011-10-27T01:37:39.877Hmm it looks like that filters to include the dirs i'm trying to exclude. If I run this: – Andy Arismendi – 2011-10-27T15:05:31.303
When the preceding
-name
or-path
term matches,-prune
tellsfind
to stop searching that branch of the file system tree. Read the find(1) man page, the section on the-prune
option for a better explanation. I tested that command briefly on my Fedora 14 system and it seemed to work properly. – garyjohn – 2011-10-27T15:43:05.220I needed to add a few more directories to the exclusion list and that syntax is just too hard to read. I wrote a python script to do something similar. Thanks for your help Gary! – Andy Arismendi – 2011-10-30T20:15:20.377