Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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."
share|improve this question

1 Answer 1

up vote 1 down vote accepted

GNU grep, which is what you're using if you're running Linux, will do recursive searches by itself, without the need for find and xargs. It also has an --exclude-dir option to "Exclude directories matching the pattern DIR from recursive searches." So you could write your search as

grep -R --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/tmp --exclude-dir=/lib whatever /


Update following comment:

If your grep doesn't support -R or --exclude-dir, then you might have to resort to find and xargs, which work fine, but have messy syntax for this task. I seldom get it right the first time.

find / -path /sys -prune -o \( -path /proc -prune -o \( -path /tmp -prune -o \( -path /lib -prune \) \) \) -o -print0 | xargs -0 grep whatever
share|improve this answer
    
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: unixhelp.ed.ac.uk/CGI/man-cgi?grep . –  Andy Arismendi Oct 27 '11 at 1:37
    
Hmm it looks like that filters to include the dirs i'm trying to exclude. If I run this: –  Andy Arismendi Oct 27 '11 at 15:05
    
When the preceding -name or -path term matches, -prune tells find 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 Oct 27 '11 at 15:43
    
I 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 Oct 30 '11 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.