2 Answers
2

Using grep -r blindly on / is not a good idea. Several directories (e.g. /dev and /proc) contain special files that should not be accessed in an uncontrolled manner - doing so could just flood your screen with errors, have you wait until the end of the world or even crash your system.

You need to use find to prevent the search from descending into those directories and leave special files alone:

Use the -xdev option to avoid descending to other filesystems completely:

find / -maxdepth 2 -xdev -type f -exec grep "xxx" {} +

-type f will only let through regular files. You can use as many -path and/or -prune options as you need to fine-tune the output of find.

Also note the use of the -exec ... + variation of -exec that calls grep with multiple files, rather than launch a separate grep process for every single one of them.
Alternatively, you could use xargs to call grep:

1. find ... -exec ... ; is a very bad idea; it will launch a new grep process for each matching file. 2. The reason grep ... /dev/null prints out the file name is because you force grep into multi-file mode by adding /dev/null as a second file. grep -H is the proper way to do this...
–
thkalaJul 11 '12 at 21:14

@thkala grep -H is not the proper way. the -H switch is a non-standard GNU extension see HP-UX and AIX
–
dadinckJul 13 '12 at 15:37

If you are doing this as a one time command, make sure to "nice" it. If you will be doing this often, generate a proper script with "find2perl"
–
dadinckJul 13 '12 at 15:41