I have so many systems I need to do and I find it really impractical for me to grep everything/folders individually.
Asked
Active
Viewed 120 times
1 Answers
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
find / \( -path /proc -o -path /<other> \) -prune -o -type f -exec grep "pattern" {} /dev/null +. The+means you'll rungrepagainst groups of files rather than every single time a file is found;/dev/nullmakes suregrepprints 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+, but why not just use the-Hflag to grep instead of including/dev/null? – Eric Renouf May 15 '15 at 04:07-Hflag is that, at least in POSIX, the/dev/nullwould 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... -exec grep "pattern" /dev/null {} +. As for-H, somegrep(1)implementations might not have it, while the/dev/nulltrick should work everywhere. shrug – lcd047 May 15 '15 at 04:38