LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   To recursively search with grep (https://www.linuxquestions.org/questions/slackware-14/to-recursively-search-with-grep-384723/)

grautu 11-19-2005 05:13 PM

To recursively search with grep
 
To put grep search a certain pattern (say chroot) on the entire partition / I use (as root) the following command
Code:

# grep -r chroot /
Consequently I get the answer
Code:

Binary file /bin/su matches
Binary file /bin/login matches
_

where "_" is a blinking cursor. That is the system hangs and cannot be restarted but with brutal keystrokes (such as ctrl+alt+del etc). I guess that happens due to some special files which grep cannot read. Could you please help me to adapt the grep command to search a certain pattern (say string) within "usual" files (say nonexecutable, non-symlinks, non-?) and throughout the root partition / ?
Thanks!

uselpa 11-19-2005 05:33 PM

find and xargs are your friend, like:
Code:

find / -type f | xargs grep chroot
try "man find" to see all the options.

Slim Backwater 11-19-2005 08:35 PM

Quote:

Originally posted by uselpa
find and xargs are your friend, like:
Code:

find / -type f | xargs grep chroot
try "man find" to see all the options.

I just want to add one thing. That will apparently break if any of your filenames have carriage returns in them, and is the reason why the find -print0 and the xargs -0 parameters were created.

Code:

find / -type f -print0 | xargs -0 grep chroot

grautu 11-20-2005 04:31 AM

Thanks a lot uselpa and Slim Backwater for your help!

gbonvehi 11-20-2005 12:33 PM

You could also use grep's -I option which will skip binary files. Like: grep -I -r /

loonix 11-21-2005 02:53 AM

On all pc's i have access to I add the following bash script I whipped up. Short, simple and REAL handy.
Code:

#!/bin/bash
if [ $# -eq 0 ]
then
        echo "Usage: search [-l] string"
else
        find . -name "*" -type f -print0 | xargs -0 grep $1 $2
fi

The optional -l flag is the standard grep -l flag. Just return the filenames or return the matches aswell as the filenames



Regards

Mick Pollard
aka lunix-aus
http://www.lunix.com.au


All times are GMT -5. The time now is 07:34 PM.