LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2007, 11:08 AM   #1
sirius56
Member
 
Registered: Nov 2006
Location: Illinois
Distribution: Ubuntu 10.4
Posts: 35

Rep: Reputation: 15
Search for string in entire filesystem?


Is there a way to search for a string in my entire filesystem? Not only filenames, but (non binary) text within files.

I tried

grep string /
grep string *
grep string

but these do not seem to work.

Thank you.
 
Old 04-28-2007, 11:18 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch
Posts: 1,594

Rep: Reputation: 97
Hya,

I understand what you want to do. However, I do not recommend to proceed. While you check /dev /proc files, your Penguin may choke.

If you want to proceed, do at your risk. Look for -r option in grep.

Happy Penguins!

PS. Change your mind! Specify the directory you want to check. and If you want to exclude binary files, read manual for "find." It would be something like
Code:
find /your/search/path -exec grep string {} \; -print
Modify commands as necessary.

Last edited by kaz2100; 04-28-2007 at 11:25 AM.
 
Old 04-28-2007, 04:43 PM   #3
sirius56
Member
 
Registered: Nov 2006
Location: Illinois
Distribution: Ubuntu 10.4
Posts: 35

Original Poster
Rep: Reputation: 15
Also went to root and tried

grep -r 'string' * (i.e., recursive)


Not quite sure if this worked because the search never ended.
 
Old 04-28-2007, 05:31 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by sirius56
grep -r 'string' * (i.e., recursive)


Not quite sure if this worked because the search never ended.
That's because the command recurses into the /dev directory and likely tries to read from your current TTY, but never gets an EOF (because you never type it in at the keyboard). This is the problem (or something similar) that kaz2100 was referring to.

Wanting to search for a string within files and in the filename itself will require either:
1) Two searches or
2) A shell script

For the two search method, I would do something like:
Code:
$ find / -type f \
         -path /dev -prune -o \
         -path /proc -prune -o \
         -path /srv -prune -o \
         -path /sys -prune -o \
         -print | grep "string_to_look_for"

$ find / -type f \
         -path /dev -prune -o \
         -path /proc -prune -o \
         -path /srv -prune -o \
         -path /sys -prune -o \
         -exec grep -l "string_to_look_for" {} \;
The first find command locates all files (-type f) on your system that are not in /dev, /proc, /srv, or /sys, prints their filenames, and then grep looks for a match to the string you are searching for. The second command does something similar, but it checks the contents of all your files for the same string. It will print out the filename of any file that contains a match.

To accomplish the same task with a single find command, you need a shell script. Something like this:
Code:
#!/bin/bash

if [ -z "${1}" ] ; then
  echo "This script expects a filename argument!"
  exit 1
fi

if [ -n "$( echo "${1}" | grep "string_to_look_for" )" ] ; then
  filename_match=1
else
  filename_match=0
fi

if [ -n "$( grep "string_to_look_for" "${1}" )" ] ; then
  file_content_match=1
else
  file_content_match=0
fi

if [ \( ${filename_match} -eq 1 \) -o \( ${file_content_match} -eq 1 \) ] ; then
  echo "${1}"
fi

exit 0
Make the script executable, and then issue this command:
Code:
$ find / -type f \
         -path /dev -prune -o \
         -path /proc -prune -o \
         -path /srv -prune -o \
         -path /sys -prune -o \
         -exec /path/to/new/scriptname {} \;
The script is not very pretty and I don't guarantee it will work without tinkering (because I have not tested it).

Last edited by Dark_Helmet; 05-01-2007 at 02:39 AM.
 
Old 04-30-2007, 08:10 PM   #5
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch
Posts: 1,594

Rep: Reputation: 97
Hya,

I simply cannot believe it.

Happy Penguins!
 
  


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
LXer: How to hide an entire filesystem LXer Syndicated Linux News 0 03-14-2007 06:31 AM
Rsync Entire Filesystem? sutley Linux - Newbie 4 10-04-2006 07:29 PM
PHP launches the entire database in one search?! Alexander.s Programming 5 04-28-2005 08:32 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 05:15 PM
Mount Loopback filesystem on an entire HD image? wartstew Linux - General 0 01-24-2003 08:46 PM


All times are GMT -5. The time now is 12:38 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration