I need to find all the instances of a given string in the whole filesystem, because I don't remember in which configuration files, script or any other programs I put it and I need to update that string with a new one.
I tried with the following command
`grep -nr 'needle' / –exclude-dir=.svn | mail myaddress@example.com -s 'References on xxx'
If I run this command on a small directory it gives me the output I need in the form
/path1/:nn:line containing needle
/path2/:nn:line containing needle
where /path1 is the full path of the file, nn is the row containing the needle and last field is the content of the line.
However when I run the command on the root directory the grep process hang after a while. I run this script about 8 hours ago and even on a small filesystem (less than 5GB) it doesn't end and if I run top
or ps
the process seems sleeping
root 24909 0.0 0.1 3772 1520 pts/1 S+ Feb10 0:15 grep -nr needle / --exclude-dir=.svn
Why it doesn't end? Is there any better way to do this (it's a one time job, I don't need to execute this more than once)
Thanks.
Update: I found a working solution with find and xargs which seems to be working and uses less system resources than the find -exec solution. Here is my final command line:
find /{boot,etc,home,lib,lost+found,opt,root,sbin,usr,var} -type f -print0 | xargs -r0 grep -nr 'needle' | mail myaddress@example.com -s 'References on xxx'
I used the /{dir1,dir2,...,dirn}
syntax because I want the output lines with the fullpath including the leading slash, and in this way you can use the command without cd /
i.e. from any directory.
Best Answer
There are some files in the file system that aren't real files, but are instead hooks into the kernel. Some of those can be read from forever. Try
to see this in action. Get ready to stop it with ctrl-C before it takes over the whole system.
If I wanted to do what you're doing, I'd enumerate the subdirectories of
/
that I wanted scanned, and make sure I only checked text files, probably withNote how the list does not contain
/dev
,/proc
,/sys
or/selinux
.