Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I agree that you need a better idea where to look.
You could us the "file" command. It will look only in the beginning of a file. So you would pipe the output of "find" to "file" and the use sed to filter out the added info from each line found. Then you can use xargs to process that list of files. You will probably want to use the -print0 in the find command and -0 in the xargs command. Also because the list will be so long, use one of the "xarg" arguments to limit how much is processes at once.
Besides having an idea where to look, there are some directories where you don't want to look such as /sys, /proc, /mnt/.
Linux does not enforce file name extensions as file-type identifiers. Searching for files ending in .txt will probably find you a list of some text files.
using find's -exec option works fine, but it is very inefficient for large numbers of files. This is because the specified command is invoked once per found file. If you have thousands of files, that means thousands of invocations of grep or whatever else you are running.
Since grep can search through many files at once if they are listed one after the other on the command line, a better approach is to use xargs. xargs read a command and list of strings, and executes command for groups of the listed strings.
Consider this example:
$ seq 1 10 |xargs -n 3 echo
1 2 3
4 5 6
7 8 9
seq just prints number 1 to 10. You can see that xargs is grouping them into threes and appending hem a arguments to echo. You can do the same thing with find:
find / -type f |xargs grep -l "string1"
However, there is a potential problem... If a file name contains a space, grep will treat it as two separate file names (because the space character is an argument de-limiter). You can protect against this by asking find to delimit its output using an ASCII NUL by using the -print0 option to find and you can tell xargs to expect this using the -0 option. It makes the command a little longer by a lot more robust:
find / -type f -print0 |xargs -0 grep -l "string1"
Linux's filesystem is arranged in a way which groups files by type. This provides a mechanism for avoiding large binary files which you don't want to search. For example, all user files should be in /home somewhere. This is where all your documents and photos and so on should reside. So if you want to search only your work, and not through the files which are installed as part of software packages, you can start your search here:
find /home -type f -print0 |xargs -0 grep -l "string1"
This will save a lot of time. You might also want to avoid anything inside any directory named bin. There are several ways to do this. You can use grep to remove any paths with /bin/ in them for example which will stop the search from bothering with any user-installed programs (which are often placed in $HOME/bin). Remember we're using the NUL delimited output from find. grep takes then option -z to understand this:
You may also consider the option -I of grep, which is equivalent to the long option
--binary-files=without-match. This will process only the first bytes of a file, just to assume it is a binary and then ignore it.