-1

I have so many systems I need to do and I find it really impractical for me to grep everything/folders individually.

derpyderp
  • 101

1 Answers1

0

There may be more elegant solutions to find what you're looking for than trying to grep everything in a filesystem, but you could use find to help and build a list of places to exclude like

find / \( -path /proc -o -path /<other> \) -prune -o -type f -exec grep -H "pattern" {} +

or something like that. It sure feels like there may be a better way to solve the problem of finding what you're looking for than this though.

Update: Based on comment from @lcd047 I added a flag to get grep to show you where it was matching stuff and + to make the exec part of find more efficient

Eric Renouf
  • 18,431
  • You're close. Try this instead: find / \( -path /proc -o -path /<other> \) -prune -o -type f -exec grep "pattern" {} /dev/null +. The + means you'll run grep against groups of files rather than every single time a file is found; /dev/null makes sure grep prints the filename even when searching a single file. Alternatively: find / \( -path /proc -o -path /<other> \) -prune -o -type f -print0 | xargs -0 grep "pattern" /dev/null. – lcd047 May 15 '15 at 04:05
  • I should use the +, but why not just use the -H flag to grep instead of including /dev/null? – Eric Renouf May 15 '15 at 04:07
  • @lcd047 Another reason to use the -H flag is that, at least in POSIX, the /dev/null would negate much of the advantage of the + I think. I think + is only special when it follows {} which it would not do in your example, though perhaps some implementations would be more efficient there nonetheless – Eric Renouf May 15 '15 at 04:17
  • 1
    Right, it should be ... -exec grep "pattern" /dev/null {} +. As for -H, some grep(1) implementations might not have it, while the /dev/null trick should work everywhere. shrug – lcd047 May 15 '15 at 04:38