find a string in all ASCII files of a system
Hi,
I know that find . -type f -exec grep -il 'string1' {} \; will look for string1 in all regular files. However, it will also look in binary files, which takes to much time. I only want the command to look in ASCII / TEXT files only. I find a lot of examples like find . -name "*.txt* -exec grep ... but this command restricts the search to *.txt files only, but there are can be other ASCII files who do not have the the .txt extension and can also contain the info i am looking for/ I try to include the file command, as this command tells me the type of a file, but I am struggling to have this command integrated into the find statement. can anybody help me ? the idea is lauch the entire query from / so that I can look for parameters in configfiles without noing what configfiles are used. This helps exploring a linux distro a lot. |
You can integrate command#1 into command#2 if #1 produces exactly what #2 is looking for. In this case, I think you will need to actually write a small script.
BUT: config files are typically only in certain directories--eg /etc, $HOME, and few others. Thus is seems more efficient to simply restrict the search to those directories |
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/. |
A few points:
|
Thank you Matthewg42.
Your solution is indeed a quick and was the one I am looking for. |
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. |
All times are GMT -5. The time now is 01:34 AM. |