LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 11-19-2005, 05:13 PM   #1
grautu
Member
 
Registered: Jul 2005
Posts: 139

Rep: Reputation: 15
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!

Last edited by grautu; 11-19-2005 at 05:14 PM.
 
Old 11-19-2005, 05:33 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
find and xargs are your friend, like:
Code:
find / -type f | xargs grep chroot
try "man find" to see all the options.
 
Old 11-19-2005, 08:35 PM   #3
Slim Backwater
Member
 
Registered: Nov 2005
Distribution: Slackware 10.2 2.6.20
Posts: 68

Rep: Reputation: 15
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
 
Old 11-20-2005, 04:31 AM   #4
grautu
Member
 
Registered: Jul 2005
Posts: 139

Original Poster
Rep: Reputation: 15
Thanks a lot uselpa and Slim Backwater for your help!
 
Old 11-20-2005, 12:33 PM   #5
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
You could also use grep's -I option which will skip binary files. Like: grep -I -r /
 
Old 11-21-2005, 02:53 AM   #6
loonix
Member
 
Registered: Aug 2003
Location: Sydney Australia
Distribution: CentOS/archlinux
Posts: 40

Rep: Reputation: 16
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can you specify which files to grep search? sneakyimp Linux - Software 4 10-12-2005 08:28 PM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM
grep search in zip/rar... etc plainkeyman Linux - Software 2 05-23-2005 06:05 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 03:29 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM


All times are GMT -5. The time now is 09:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration